fix bugs and add tests for 100% coverage