At the heart of this was a subtle indexing error due to calling
round() instead of math.floor(). Fixing that needed a cascade of
fixes to expectated values in various tests.
Finally, core logic in the merge command was rewritten for
correctness and clarity.