added inf loop detection