Fix bugs found by unit tests.