fixed inifinite loop detection