Add detection of infinite loops and self-loops.