Refactored to eliminate duplicate code