add tests, fix a bug