fixed the loop bug