move more common code out of duplicated functions