extract duplicated code