REVIEW-3313: Broke out classes to make them more testable. Added unit tests.