fixed for loop index problem