Refactor to eliminate duplicate code