add tests, fix bugs