Refactor change detection

WIP
This is a bug. I've added a test reproducing it.