Fixed looping problem