refactor out duplicate code