Finished refactoring and tests