Fixing endless loop