Avoid SO with cyclic dependencies

Avoid following cyclic dependencies and make sure there are no duplicates in the resulting list of dependencies.