Add tests and fix bugs that tests found