add tests and bug fixes