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.
`handleEntry` builds up a pretty big call stack as it's called recursively over each entry in a data file.  Wrapping recursive calls in a `setImmediate` function seemed to fix it.  Not sure if there's a better way, but it works for now.