corrected function for removing 1st loop