add tests and fixed bugs