implemented #154 remove cyclic dependencies between functions

This is just a first try, probably very bugged.
There are just two simple test cases.

And it could still need some optimization, for example binary search for the function, or
better use of return values.