Add more tests and fix bugs