Fixed for loop bug