Add test coverage and fix minor bugs