More refactoring and some more test cases