Fixed another bug with loop detection