Add a better error message in case of cyclic dependencies.

This should help debugging issues like #438
Fix a nasty bug in the solver with possible cyclic dependencies (this should fix #438)