add unit tests and fix bugs