Thanks Adam Chidlow for polishing the patch. Backport of 03049fb8d96ccd1f1ed0285486103542de42faba from master