Fixed for loop index error