Resolve issues 4366 and 4420 for better patch performance

I rebased the last two commits here on another change in my PR for branch 'test'. If you take this PR no need for that one. The test changes were necessary but otherwise unrelated, so they were a separate commit.
No problem. I'm running some tests now to make sure other things work (now that all the results are combined - despite using Git, internally changes are few enough that code flow is basically linear) and will submit a post-merge fix up PR.