Fix loop detection