Thanks to Aron Podrigal for the initial patch and Tim for the review. Backport of 65e005f8 from master