Refactor, format, and add tests.