add tests and fix bugs