Add test coverage, fix bugs found by tests.