pull out common code so it's not duplicated