Fix inifinite loop problem