Some unit tests for various code

Misc unit tests and small clean-ups.