Refactor and optimize the way the external and internal relations are fetched. refs #18153