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.
Without this change, the tests that writes the same key multiple times failed.