Closes issue #11550. Thanks to Ben Hayden for some inspiration on the solution.
Thanks to Mark Smith for the patch.