Corrected errors in loop