Fix critical looping bug