Fix infinite loop on self-references