Adding the correct algorithm for circular references