Broke some logic up into inline functions