refactor nearly duplicate code