cleanup and bugfix

Cleaned up a few things and fixed a bug as you can see in the commit message.

Also was trying to debug a crash/unstable activity when syncing a lot of pending messages (> 300) after an import all at once.  I should/will open an issue for that soon unless you want to try to replicate that.