Fix loop detect 740

:facepunch: 

Also added a debug flag that can be switched on or off. Would be nice of mocha could turn this on automatically, but it'll do for now.