corrected errors in for loop