clean up the code and write more test cases