add tests and fix bugs found