added test cases and more refactoring