add tests, fix some bugs