refactored a bit to clean things up for testing.