Simplify reference updating, add more assertions.

Since all states should believe in the same number of variable references, we can simplify the logic here a bit.

While we're at it, add some more assertions documenting our assumptions.