adding loop detection and including tests for this feature