Finished refactoring and fixed tests