refactor duplicated code