Add more tests and cleanup the code