Fix mid loop bug