Improve test coverage, and fix issues found by new tests