Refactor. Add unit tests.