fix some bugs related to compact()

Hey, just a heads up - I do not know what I'm doing here...
But the changes that I've added made the failing test cases work.

This is just a first commit, I'll take a closer look later to see if the changes I've done got some sideeffects. And get some more tests written.
To reproduce the issue, put 100,000 entries, overwrite those same 100,000 entries, and run compaction.  The dreaded `RangeError: Maximum call stack size exceeded` rears its ugly head.