Keep references; more sophisticated reference cycle detection
A loop is no longer required for reference resolution

We now use recursion instead.
Increase difficulty of cyclic reference detection test
Fix error message + improve tests
Refactor code for consistency