Refactoring and improved test cases