loop bug fix