refactored to make easier to test