Improve testability and add unit tests.
Improve testability and add unit tests.