Rewrite submodules code to be more robust.