optimized loop and fixed indentation