optimized code to reduce complexity