Refactored to eliminate code duplication