Fixing loop bug