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.
Thanks! I've added a test to medea_compact that did what you suggested (I managed to reproduce it with 25 000 entries though). Fixed it by putting in in a `setImmediate` as you suggested.