Code clean up and add a few more test cases