docs: def _refine_merge(merge, aliases, min_goodness):
      """Remove entries from a merge to generate a valid merge which may be
      applied to the routing table.
  
      Parameters
      ----------
      merge : :py:class:`~.Merge`
          Initial merge to refine.
      aliases : {(key, mask): {(key, mask), ...}, ...}
          Map of key-mask pairs to the sets of key-mask pairs that they actually
          represent.
      min_goodness : int
          Reject merges which are worse than the minimum goodness.
  
      Returns
      -------
      :py:class:`~.Merge`
          Valid merge which may be applied to the routing table.
      """
      # Perform the down-check
      merge = _refine_downcheck(merge, aliases, min_goodness)
  
      # If the merge is still sufficiently good then continue to refine it.
      if merge.goodness > min_goodness:
          # Perform the up-check
          merge, changed = _refine_upcheck(merge, min_goodness)
  
          if changed and merge.goodness > min_goodness:
              # If the up-check removed any entries we need to re-perform the
              # down-check; but we do not need to re-perform the up-check as the
              # down check can only move the resultant merge nearer the top of
              # the routing table.
              merge = _refine_downcheck(merge, aliases, min_goodness)
  
      return merge