[FIX] Filesystem logic for complicated paths

    Previous verison was not matching file and module paths correctly
    Pulled out some functionality into separate functions