Fixing a loop issue