Refactored to reduce complexity