Fixed loop bug