Add more tests and refactor code