Well, I am not saying this code does not work — rather that it maybe can be reformulated clearer, to not depend on current subtle «selectable» invariants and so on. Maybe it requires introduction of some helper functions.