refactored to reduce complexity