anchor.diff:                  lists:keyreplace(Global, 1, Acc, {Global, OldV + V})
           end,
   
  -    Acc2 = [{PerIndex, V} | Acc1],
  -
  -    do_aggregate_index_bucket_stats(Service, Acc2, Bucket, Rest);
  -do_aggregate_index_bucket_stats(_Service, Acc, _, Stats) ->
  -    {finalize_index_stats(Acc), Stats}.
  -
  -finalize_index_stats(Acc) ->
  +    Acc2 = [{PerItem, V} | Acc1],
  +
  +    do_aggregate_bucket_stats(Service, Acc2, Bucket, Rest);
  +do_aggregate_bucket_stats(_Service, Acc, _, Stats) ->
  +    {finalize_stats(Acc), Stats}.
  +
  +finalize_stats(Acc) ->
       lists:keysort(1, Acc).
   
  -aggregate_index_stats_test() ->
  +aggregate_stats_test() ->
       In = [{{<<"a">>, <<"idx1">>, <<"m1">>}, 1},
             {{<<"a">>, <<"idx1">>, <<"m2">>}, 2},
             {{<<"b">>, <<"idx2">>, <<"m1">>}, 3},
             {{<<"b">>, <<"idx2">>, <<"m2">>}, 4},
             {{<<"b">>, <<"idx3">>, <<"m1">>}, 5},
             {{<<"b">>, <<"idx3">>, <<"m2">>}, 6}],
  -    Out = aggregate_index_stats(service_index, In, [], []),
  +    Out = aggregate_stats(service_index, In, [], []),
   
       AStats0 = [{<<"index/idx1/m1">>, 1},
                  {<<"index/idx1/m2">>, 2},
  
positive.code:                 not_found ->
                      undefined;
                  _ ->
                      {Type, {Bucket, Index, Metric}}
              end;
          _ ->
              undefined
  
negative.code: 
      {ok, #state{service = Service,
                  buckets = Buckets,
                  default_stats = finalize_stats(Defaults)}}.
  
  find_type(_, []) ->
      not_found;
  