Refactored to reduce comlexity