Slight refactoring to allow for easier testing.