Refactor change detection

WIP
I wonder if a util function `function glue(r1, r2) { r1.next = r2; r2.prev = r1; }` would make a few of the algorithms here more readable. Up to you.