add circular reference detection & refactorings