Fix multiple dependency resolving flaws

Found two issues, when analyzing larger project:
1) ran out of stack size: `Exception caught:  { '0': [RangeError: Maximum call stack size exceeded] }`
2) got infinite loop on cyclic dependency

This patch
- uses stack instead of recursion
- checks for cyclomatic dependencies