Fix loop bug