Patch to follow in the next post.
Created attachment 20271 [details] Proposed patch
An improved version of this was landed.