               term: state.term,
               leader: Pontoon.Membership.get_own_name(),
               prev_log_idx: state.commit_idx,
-              prev_log_term: state.term,
+              prev_log_term: State.get_last_log_term(state),
               leader_commit: nil,
               entries: entries,
         })


            {role, term} =
                if vote_count >= majority_votes do
                  Logger.info("Received majority #{vote_count}/#{majority_votes}! Seizing power")
                  {:leader, state.term + 1}
                else
                  Logger.info("election not over yet, #{vote_count}/#{majority_votes}")
                  {:candidate, state.term}
                end


      Process.send_after(pid, {:append_entries, []}, @append_entries_interval_ms)
    end

    def get_last_log_term(state) do
      case List.last(state.log) do
        nil -> 0
        tl -> tl.term
      end
    end

    def maybe_step_down(state, member, remote_term) do
      if state.term < remote_term do
        Logger.info("#{member.name} has higher term! Stepping down")
        %{state | term: remote_term, role: :follower, voted_for: nil, leader: member.name}
      else
        state
      end
    end

    # AppendEntries RPC implementation.
    def handle_rpc(state, member, %RPC.AppendEntries{} = rpc) do
      state = maybe_step_down(state, member, rpc.term)
      |> State.reset_election_timer(self())

      {accept_append, state} =
