anchor.diff:      {{_LeastMbr, LeastPos}, LeastRest} = least_expansion(
               Fd, SubtreeMbr, EntriesPos),
       LeastRestPos = [Pos || {Mbr, Pos} <- LeastRest],
  -?debugVal(length(LeastRestPos)),
       case insert_subtree(Fd, LeastPos, Subtree, Level, Depth+1) of
       {ok, NewMbr, NewPos, Inc} ->
           MergedMbr = vtree:merge_mbr(ParentMbr, NewMbr),
  -?debugVal(length([NewPos|LeastRestPos])),
           NewNode = {MergedMbr, #node{type=inner}, [NewPos|LeastRestPos]},
           {ok, Pos} = couch_file:append_term(Fd, NewNode),
           {ok, NewMbr, Pos, Inc};
  
positive.code:     end, NewNodes),
  
  
      ?debugVal(NewChildren),
      ?debugVal(length(NewChildren)),
  
      ?debugVal(Disjoint),
      DisjointMbrAndPos = [{Mbr, Pos} || {Mbr, Pos, _} <- Disjoint],
      ?debugVal(DisjointMbrAndPos),
  
      DisjointMbrAndPos ++ NewChildren.
      
  
  % Inner node, do some repacking.
  seedtree_write_insert2(Fd, Orig, OmtTree, OmtHeight) ->
      %?debugVal(Orig),
      ?debugVal(OmtHeight),
  
  % NOTE vmx (2010-12-07): this is only for debugging
      OrigLeafDepths = hd(vtreestats:leaf_depths(Fd, hd(Orig))),
      ?debugVal(OrigLeafDepths),
      case (OrigLeafDepths+1 /= OmtHeight) and (OrigLeafDepths > 0) of
      true ->
          io:format(user, "WARNING! Seedtree leaf nodes to match up with OMT tree!~n", []);
      false ->
          ok
      end,
  
      %?debugVal(OmtTree),
      % write the OmtTree to to disk.
      {ok, OmtMbrAndPos} = omt_write_tree(Fd, OmtTree),
      ?debugVal(length(OmtMbrAndPos)),
      ?debugVal(OmtMbrAndPos),
  
      % Get the enclosing MBR of the OMT nodes
      {OmtMbrs, _OmtPos} = lists:unzip(OmtMbrAndPos),
      OmtMbr = vtree:calc_mbr(OmtMbrs),
      % We do some repacking for better perfomance. First get the children
      % of the target node where the new nodes will be inserted in
      TargetChildren = load_nodes(Fd, Orig),
  
      % Transform the target nodes from normal ones to tuples where the
      % meta information is replaced with the position of the node in
      % the file.
      TargetMbrAndPos = lists:zipwith(fun({Mbr, _, Children}, Pos) ->
          {Mbr, Pos, Children}
      end, TargetChildren, Orig),
      %?debugVal(TargetMbrAndPos),
  
      % Get all nodes that are within or intersect with the input tree
      % root node(s)
      {Disjoint, NotDisjoint} = lists:partition(fun({Mbr, _Pos, _}) ->
          vtree:disjoint(Mbr, OmtMbr)
      end, TargetMbrAndPos),
  %    ?debugVal(Disjoint),
  %    ?debugVal(NotDisjoint),
  
      % Get the children of those nodes, that intersect the input MBR,
      % so that they can be repacked for better query performance
      PosToRepack = [Children || {_Mbr, _Pos, Children} <- NotDisjoint],
      ?debugVal(PosToRepack),
  
      % And reduce the disjoint nodes to MBR and position in file only
      DisjointMbrAndPos = [{Mbr, Pos} || {Mbr, Pos, _Children} <- Disjoint],
  ?debugVal(DisjointMbrAndPos),
      % Get the actual nodes that should be repacked
      %{NodesToRepack, OmtMbrAndPos2, DisjointMbrAndPos2} = case lists:append(PosToRepack) of
      %PosToRepack2 when is_tuple(hd(PosToRepack2)) ->
      PosToRepack2 = lists:append(PosToRepack),
      {NodesToRepack, OmtMbrAndPos2, DisjointMbrAndPos2} = case PosToRepack2 of
      [] ->
  ?debugMsg("empty list"),
          OmtLeafsList = lists:map(fun({_, OmtLeafsPos}) ->
              {ok, {Mbr, _, OmtLeafs}} = couch_file:pread_term(Fd, OmtLeafsPos),
              {Mbr, OmtLeafs}
          end, OmtMbrAndPos),
  ?debugVal(OmtLeafsList),
  %        OmtLeafsMbrAndLeaf = lists:append(OmtLeafsList),
  %?debugVal(OmtLeafsMbrAndLeaf),
          %{PosToRepack2, OmtMbrAndPos, DisjointMbrAndPos};
  %        {PosToRepack2, OmtLeafsMbrAndLeaf, DisjointMbrAndPos};
          {PosToRepack2, OmtLeafsList, DisjointMbrAndPos};
      PosToRepack2 when is_tuple(hd(PosToRepack2)) ->
  ?debugMsg("reached leaf nodes"),
          % Reached leaf nodes
          % OMT tree nodes are on the same level as those nodes =>
          % OMT tree nodes are leaf nodes => load those nodes
  %        [{_, OmtLeafsPos}] = OmtMbrAndPos,
  %        {ok, {_, _, OmtLeafs}} = couch_file:pread_term(Fd, OmtLeafsPos),
          OmtLeafsList = lists:map(fun({_, OmtLeafsPos}) ->
              {ok, {_, _, OmtLeafs}} = couch_file:pread_term(Fd, OmtLeafsPos),
              OmtLeafs
          %end, OmtMbrAndPos),
          end, OmtMbrAndPos),
          %?debugVal(OmtLeafsList),
  %        OmtLeafsMbrAndLeaf = [{Mbr, Leaf} || {Mbr, _, _}=Leaf <- OmtLeafs],
          %OmtLeafsMbrAndLeaf = [{Mbr, Leaf} || {Mbr, _, _}=Leaf <- hd(OmtLeafsList)],
          OmtLeafsMbrAndLeaf = [{Mbr, Leaf} || {Mbr, _, _}=Leaf <-
                  lists:append(OmtLeafsList)],
          %?debugVal(OmtLeafsMbrAndLeaf),
          %{PosToRepack2, OmtLeafsMbrAndLeaf};
  %        DisjointLeafsList = lists:map(fun({_, DisjointLeafsPos}) ->
  %            {ok, {_, _, DisjointLeafs}} = couch_file:pread_term(Fd, DisjointLeafsPos),
  %            DisjointLeafs
  %        %end, OmtMbrAndPos),
  %        end, DisjointMbrAndPos),
  %        DisjointLeafsMbrAndLeaf = [{Mbr, Leaf} || {Mbr, _, _}=Leaf <-
  %                lists:append(DisjointLeafsList)],
          %{PosToRepack2, OmtLeafsMbrAndLeaf, DisjointLeafsMbrAndLeaf};
          {PosToRepack2, OmtLeafsMbrAndLeaf, DisjointMbrAndPos};
       PosToRepack2 ->
  ?debugMsg("else"),
          %load_nodes(Fd, PosToRepack2)
          %{load_nodes(Fd, PosToRepack2), OmtMbrAndPos}
          %{load_nodes(Fd, PosToRepack2), OmtMbrAndPos, DisjointMbrAndPos}
  %        DisjointLeafsList = lists:map(fun({_, DisjointLeafsPos}) ->
  %            {ok, {_, _, DisjointLeafs}} = couch_file:pread_term(Fd, DisjointLeafsPos),
  %            DisjointLeafs
  %        %end, OmtMbrAndPos),
  %        end, DisjointMbrAndPos),
  %        DisjointLeafsMbrAndLeaf = [{Mbr, Leaf} || {Mbr, _, _}=Leaf <-
  %                lists:append(DisjointLeafsList)],
  
          DisjointLeafsList = case DisjointMbrAndPos of
          [] ->
              [];
          _ ->
              lists:map(fun({_, DisjointLeafsPos}) ->
                  {ok, {Mbr, _, DisjointChildren}} = couch_file:pread_term(Fd, DisjointLeafsPos),
                  %{Mbr, DisjointLeafs}
                  ChildNodes = load_nodes(Fd, DisjointChildren),
                  ChildNodesMbr = [Mbr || {Mbr, _, _} <- ChildNodes],
                  lists:zip(ChildNodesMbr, DisjointChildren)
              end, DisjointMbrAndPos)
          end,
          ?debugVal(DisjointLeafsList),
  
          {load_nodes(Fd, PosToRepack2), OmtMbrAndPos, lists:append(DisjointLeafsList)}
  %        {load_nodes(Fd, PosToRepack2), OmtMbrAndPos, DisjointMbrAndPos}
  
  %        % Get the children of the OMT tree root nodes
  %        OmtLeafsList = lists:map(fun({_, OmtLeafsPos}) ->
  %            {ok, {_, _, OmtLeafs}} = couch_file:pread_term(Fd, OmtLeafsPos),
  %            OmtLeafs
  %        %end, OmtMbrAndPos),
  %        end, OmtMbrAndPosFoo),
  %        OmtLeafsMbrAndLeaf = [{Mbr, Leaf} || {Mbr, _, _}=Leaf <-
  %                lists:append(OmtLeafsList)],
  %        {load_nodes(Fd, PosToRepack2), OmtLeafsMbrAndLeaf}
      end,
  ?debugVal(DisjointMbrAndPos2),
      %PosToRepack2 = lists:append(PosToRepack),
      %NodesToRepack = load_nodes(Fd, PosToRepack2),
      % Transform the nodes to repack from normal ones to tuples (consisting
      % of the MBR and the position where the node in the file (or the nodes
      % themselves if they are leaf nodes)
      ToRepackMbrAndPos = lists:zipwith(fun({Mbr, _, _}, Pos) ->
          {Mbr, Pos}
      end, NodesToRepack, PosToRepack2),
  ?debugMsg("still here"),
  %    ?debugVal(ToRepackMbrAndPos),
      % XXX BUG vmx: here's the problem. NewNodesHeight gets 3!
      % XXX vmx: might not be the problem. But perhaps that NewNodes2 > ?MAX_FILLED
      % NOTE vmx: I don't think here's where the bug is. Perhaps the height was
      %     estimated wrongly.
      % Building an OMT tree is used as split algorithm. The advantage over
      % a normal one (line Ang/Tan) is that it can split several levels at
      % one time (and not only split into two partitions)
  %?LOG_INFO("Here's the bug: ~p~n~p", [ToRepackMbrAndPos, OmtMbrAndPos2]),
      %{NewNodes, NewNodesHeight} = omt_load(
      %    ToRepackMbrAndPos ++ OmtMbrAndPos2, ?MAX_FILLED),
  ?debugVal(ToRepackMbrAndPos),
  ?debugVal(OmtMbrAndPos2),
  ?debugVal(ToRepackMbrAndPos ++ OmtMbrAndPos2),
      Foobar = omt_load(
          %ToRepackMbrAndPos ++ OmtMbrAndPos, ?MAX_FILLED),
          ToRepackMbrAndPos ++ OmtMbrAndPos2, ?MAX_FILLED),
  ?debugVal(Foobar),
  ?debugMsg("still here"),
       {NewNodes, NewNodesHeight} = Foobar,
  %    ToRepackMbrAndPos2 = if
  %        is_list(ToRepackMbrAndPos) -> ToRepackMbrAndPos;
  %        true -> [ToRepackMbrAndPos]
  %    end,
  %    OmtMbrAndPos3 = if
  %        is_list(OmtMbrAndPos2) -> OmtMbrAndPos2;
  %        true -> [OmtMbrAndPos2]
  %    end,
  %    %{NewNodes, NewNodesHeight} = omt_load(
  %    %    ToRepackMbrAndPos2 ++ OmtMbrAndPos3, ?MAX_FILLED),
  %    {NewNodes, NewNodesHeight} = omt_load(
  %        lists:append(ToRepackMbrAndPos2, OmtMbrAndPos3), ?MAX_FILLED),
      %?debugVal(NewNodes),
  ?debugMsg("still here"),
      ?debugVal(NewNodesHeight),
  
      % The tree has a height of 3 if an additional split is needed. If
      % the tree has a height of 2 an additional split might not be needed,
      % but still can be needed (especially if there a disjoint nodes).
      % Therefore collect those nodes and see if the split is needed when
      % all nodes can be considered.
      NewNodes2 = case NewNodesHeight of
      1 ->
          [NewNodes];
          %[DisjointMbrAndPos|[NewNodes]];
      2 ->
          NewNodes;
          %[DisjointMbrAndPos|NewNodes];
      %3 -> 
      %    lists:append(NewNodes)
      NewNodesHeight ->
          % flatten list
          Foo = lists:foldl(fun(_I, ToAppend) ->
              lists:append(ToAppend)
          end, NewNodes, lists:seq(3, NewNodesHeight)),
          %?debugVal(Foo),
          Foo
          %[DisjointMbrAndPos|Foo]
      end,
      ?debugVal(NewNodes2),
  %    ?debugVal(length(NewNodes2)),
  
  
  
      % Disjoint nodes are on the same level as the non-disjoint nodes. For the
      % final output, we need to create parent nodes for both of them.
  %?debugVal(DisjointMbrAndPos),
  %    NewNodes3 = case DisjointMbrAndPos of
  %    [] ->
  %        NewNodes2;
  %    _ ->
  %        %{_, DebugDisjoint} = hd(DisjointMbrAndPos),
  %        %DebugDisjointDepth = vtreestats:leaf_depths(Fd, DebugDisjoint),
  %        %{_, DebugNewNodes} = hd(hd(NewNodes2)),
  %        %DebugNewNodesDepth = vtreestats:leaf_depths(Fd, DebugNewNodes),
  %        %DebugDiff = DebugDisjointDepth - DebugNewNodesDepth,
  %        %?debugVal(DebugDiff),
  %        %[DisjointMbrAndPos|NewNodes2]
  %        [DisjointMbrAndPos|NewNodes2]
  %    end,
  %?debugVal(NewNodes3),
  
  
      % Loop through those new nodes, calculate MBRs and write them to disk.
      % Return the Mbr and positions in file of the newly written nodes.
      % And prepend them to the disjoint nodes
      %NewNodesMbrAndPos = lists:foldl(fun(Nodes, Acc) ->
      NewChildren = lists:foldl(fun(Nodes, Acc) ->
  %        case length(Nodes) of
  %        % If it has a single root node, use that one...
  %        1 ->
  %?debugVal(Nodes),
  %            [hd(Nodes)|Acc];
  %        % ...else create a new single root node
  %        _ ->
              %?debugVal(Nodes),
              ?debugVal(Acc),
              {Mbrs, Pos} = lists:unzip(Nodes),
              %?debugVal(Pos),
              ParentMbr = vtree:calc_mbr(Mbrs),
              Meta = case is_tuple(hd(Pos)) of
                  true -> #node{type=leaf};
                  false -> #node{type=inner}
              end,
              {ok, ParentPos} = couch_file:append_term(Fd, {ParentMbr, Meta, Pos}),
              [{ParentMbr, ParentPos}|Acc]
  %        end
      %end, DisjointMbrAndPos, NewNodes2),
      end, DisjointMbrAndPos2, NewNodes2),
      %end, [], [DisjointMbrAndPos|NewNodes2]),
      %end, [], NewNodes3),
      %end, [], NewNodes2),
      ?debugVal(NewChildren),
      NewChildren.
      %NewChildren2 = [DisjointMbrAndPos|NewChildren],
      %?debugVal(NewChildren2),
      %NewChildren2.
  
  
  % @doc Creates new parent nodes out of a list of tuples containing MBRs and
  
negative.code:         % negative)
          MbrAndPos2 = seedtree_write_insert(Fd, Orig, OmtTrees,
                  (OmtHeight + HeightDiff)),
          {NewMbrs, NewPos} = lists:unzip(MbrAndPos2),
  
          % NOTE vmx: seedtree_write_finish/1 might still go crazy with
  