Improve code logic for circular dependencies. fix #526