More refactoring + tests