anchor.diff:              %% device.
               Pid ! {complete_game, random:uniform(100000000)}
       end,
  -    lists:foreach(Viewer, lists:seq(1, ?NUM_EVENTS)).
  +    lists:foreach(Viewer, lists:seq(1, ?NUM_EVENTS)),
  +    {ok, State}.
   
   %% @doc Wait for all events to be delivered in the system.
  -wait_for_events(Count, NumEvents, MaxValue0) ->
  +wait(#state{count_events=Count, num_events=NumEvents}=State) ->
       receive
  -        {event, Score} ->
  -            MaxValue = max(Score, MaxValue0),
  +        {event, _Score} ->
               case Count >= NumEvents of
                   true ->
  -                    io:format("~p events served, max is: ~p!", [Count, MaxValue]),
  -                    MaxValue;
  +                    {ok, State};
                   false ->
  -                    wait_for_events(Count + 1, NumEvents, MaxValue)
  +                    wait(State#state{count_events=Count + 1})
               end
       end.
  
positive.code: %% The number of events to sent to clients.
  -define(NUM_EVENTS, 2000).
  
  test() ->
      Self = self(),
  
      %% Create a leaderboard datatype.
      {ok, LeaderboardId} = lasp:declare({lasp_top_k_var, [2]}),
  
negative.code:     %% Read the leaderboard's current value.
      {ok, {_, _, _, Leaderboard}} = lasp:read(LeaderboardId, undefined),
  
      {ok, #state{runner=Runner,
                  leaderboard_id=LeaderboardId,
                  leaderboard=Leaderboard}}.
  
  summarize(#state{leaderboard_id=LeaderboardId}=State) ->
      %% Read the result and print it.
      {ok, FinalLeaderboard} = lasp:query(LeaderboardId),
      Final = orddict:to_list(FinalLeaderboard),
      io:format("Final Leaderboard: ~p", [Final]),
  
      {ok, State}.
  
  %% @doc Terminate any running clients gracefully issuing final
  %%      synchronization.
  terminate(#state{client_list=ClientList}=State) ->
      TerminateFun = fun(Pid) -> Pid ! terminate end,
      lists:map(TerminateFun, ClientList),
      {ok, State}.
  
  %% @doc Launch a series of client processes.
  clients(#state{runner=Runner,
                 leaderboard_id=LeaderboardId,
                 leaderboard=Leaderboard}=State) ->
      SpawnFun = fun(Id) ->
                         spawn_link(?MODULE,
                                    client,
                                    [Runner, Id, LeaderboardId, Leaderboard])
                 end,
      ClientList = lists:map(SpawnFun, lists:seq(1, ?NUM_CLIENTS)),
      {ok, State#state{client_list=ClientList}}.
  
  %% @doc Client process; standard recurisve looping server.
  client(Runner, Id, LeaderboardId, Leaderboard0) ->
  