Patch Set 2:

Hmm, after re-reading the tests it looks good to me.

Christian, please rebase your other work on this and see if the new implementation works as expected.  If it doesn't, please try to distill the problem into a new test case so we can test for it, and fix it.