fixing undefined for loop error