CRM-14197 refactor out smaller functions