Make code a bit more unittest-able.

Fix bugs found by unittests. :-)