Add more tests and bug fixes