Refactor and get tests passing.