Refactored to allow for testing.