Begin writing tests and refactoring