Fixed possible infinite loop