Fixed possible infinite loop in hasSpace