DRY branch
break up megafunction into separate pieces