Refactoring and fixing tests.