Thanks to chrisedgemon for the report and shaib, akaariai and timgraham for the review. Backport of 24ec9538 from master