Add test coverage and fix a few minor problems