Add test cases and do refactoring