fixed bug in for loop