Fixed small problem with loop detection