Finish refactor and fix failing tests