Refactored for more test friendliness.