Refactored to eliminate duplicated code