Factored out loop detection into a separate system.