Corrected error in nested loops