Refactor, add tests.