added check for circular dependencies

On a larger project, I was finding that circular dependencies were causing a stack overflow.  

I noticed that the dependency.name and dependency.version were being cached, so I added the condition to check for its existence already before continuing the flatten() recursion.