Add more tests and refactor