fixed bug in nested for loops.