Refactored to reduce duplication