refactor; all tests passing