fix blatently wrong code in path_to_top_of_module_tree function

I'll need to look into why the unit tests missed this, especially since specific tests for checking on the correctness of this function were added in #1047