fix loop index bug