Add more tests, refactor the code