bug fix to avoid looping on and removing files from the same list