Some unit tests for variouse code

Misc unit tests and small clean-ups.