Refactoring and fixing unit tests