fixed nested for loop