Refactored for unit testing.