Code refactoring and fixing test cases