refactored to more discreet functions