add tests and fix some bugs