Thanks Graham Wideman for the patch. Backport of e48a5b5a03181b3ada6183595eb6e1c599a9657a from master