Refactored to remove code duplication