More unit test coverage and code cleanup