Adding a feature to read all keys and their respective values (Java side).
[iotcloud.git] / version2 / src / java / iotcloud / Table.java
index be9defb91cef43d5921db27bd9f0ed3e9e3bb02e..6a05197fe3d68a6129c81601eaec02ef10a05650 100644 (file)
 package iotcloud;
-import java.util.HashMap;
-import java.util.Map;
+
 import java.util.Iterator;
-import java.util.HashSet;
+import java.util.Random;
 import java.util.Arrays;
+import java.util.Map;
+import java.util.Set;
+import java.util.List;
 import java.util.Vector;
-import java.util.Random;
-import java.util.Queue;
-import java.util.LinkedList;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-import java.util.Collection;
 import java.util.Collections;
 import java.nio.ByteBuffer;
 
-
 /**
- * IoTTable data structure.  Provides client inferface.
+ * IoTTable data structure.  Provides client interface.
  * @author Brian Demsky
  * @version 1.0
  */
 
 final public class Table {
-       private int numslots;   //number of slots stored in buffer
-
-       // machine id -> (sequence number, Slot or LastMessage); records last message by each client
-       private HashMap<Long, Pair<Long, Liveness> > lastmessagetable = new HashMap<Long, Pair<Long, Liveness> >();
-       // machine id -> ...
-       private HashMap<Long, HashSet<RejectedMessage> > watchlist = new HashMap<Long, HashSet<RejectedMessage> >();
-       private Vector<Long> rejectedmessagelist = new Vector<Long>();
-       private SlotBuffer buffer;
-       private CloudComm cloud;
-       private long sequencenumber; //Largest sequence number a client has received
-       private long localmachineid;
-       private TableStatus lastTableStatus;
-       static final int FREE_SLOTS = 10; //number of slots that should be kept free
+
+       /* Constants */
+       static final int FREE_SLOTS = 2; // Number of slots that should be kept free // 10
        static final int SKIP_THRESHOLD = 10;
-       private long liveslotcount = 0;
-       private int chance;
        static final double RESIZE_MULTIPLE = 1.2;
        static final double RESIZE_THRESHOLD = 0.75;
        static final int REJECTED_THRESHOLD = 5;
-       private int resizethreshold;
-       private long lastliveslotseqn;  //smallest sequence number with a live entry
-       private Random random = new Random();
-       private long lastUncommittedTransaction = 0;
-
-       private int smallestTableStatusSeen = -1;
-       private int largestTableStatusSeen = -1;
-       private int lastSeenPendingTransactionSpeculateIndex = 0;
-       private int commitSequenceNumber = 0;
-       private long localTransactionSequenceNumber = 0;
-
-       private PendingTransaction pendingTransBuild = null; // Pending Transaction used in building
-       private LinkedList<PendingTransaction> pendingTransQueue = null; // Queue of pending transactions
-       private Map<Long, Map<Long, Commit>> commitMap = null; // List of all the most recent live commits
-       private Map<Long, Abort> abortMap = null; // Set of the live aborts
-       private Map<IoTString, Commit> committedMapByKey = null; // Table of committed KV
-       private Map<IoTString, KeyValue> commitedTable = null; // Table of committed KV
-       private Map<IoTString, KeyValue> speculativeTable = null; // Table of speculative KV
-       private Map<Long, Transaction> uncommittedTransactionsMap = null;
-       private Map<IoTString, Long> arbitratorTable = null; // Table of arbitrators
-       private Map<IoTString, NewKey> newKeyTable = null; // Table of speculative KV
-       private Map<Long, Map<Long, Commit>> newCommitMap = null; // Map of all the new commits
-       private Map<Long, Long> lastCommitSeenSeqNumMap = null; // sequence number of the last commit that was seen grouped by arbitrator
-       private Map<Long, Long> lastCommitSeenTransSeqNumMap = null; // transaction sequence number of the last commit that was seen grouped by arbitrator
-       private Map<Long, Long> lastAbortSeenSeqNumMap = null; // sequence number of the last abort that was seen grouped by arbitrator
-       private Map<IoTString, KeyValue> pendingTransSpeculativeTable = null;
-       private List<Commit> pendingCommitsList = null;
-       private List<Commit> pendingCommitsToDelete = null;
-       private Map<Long, LocalComm> localCommunicationChannels;
-       private Map<Long, TransactionStatus> transactionStatusMap = null;
-
-
-       public Table(String hostname, String baseurl, String password, long _localmachineid) {
-               localmachineid = _localmachineid;
-               buffer = new SlotBuffer();
-               numslots = buffer.capacity();
-               setResizeThreshold();
-               sequencenumber = 0;
-               cloud = new CloudComm(this, hostname, baseurl, password);
-               lastliveslotseqn = 1;
 
-               setupDataStructs();
+       /* Helper Objects */
+       private SlotBuffer buffer = null;
+       private CloudComm cloud = null;
+       private Random random = null;
+       private TableStatus liveTableStatus = null;
+       private PendingTransaction pendingTransactionBuilder = null; // Pending Transaction used in building a Pending Transaction
+       private Transaction lastPendingTransactionSpeculatedOn = null; // Last transaction that was speculated on from the pending transaction
+       private Transaction firstPendingTransaction = null; // first transaction in the pending transaction list
+
+       /* Variables */
+       private int numberOfSlots = 0;  // Number of slots stored in buffer
+       private int bufferResizeThreshold = 0; // Threshold on the number of live slots before a resize is needed
+       private long liveSlotCount = 0; // Number of currently live slots
+       private long oldestLiveSlotSequenceNumver = 0;  // Smallest sequence number of the slot with a live entry
+       private long localMachineId = 0; // Machine ID of this client device
+       private long sequenceNumber = 0; // Largest sequence number a client has received
+       private long localSequenceNumber = 0;
+
+       // private int smallestTableStatusSeen = -1; // Smallest Table Status that was seen in the latest slots sent from the server
+       // private int largestTableStatusSeen = -1; // Largest Table Status that was seen in the latest slots sent from the server
+       private long localTransactionSequenceNumber = 0; // Local sequence number counter for transactions
+       private long lastTransactionSequenceNumberSpeculatedOn = -1; // the last transaction that was speculated on
+       private long oldestTransactionSequenceNumberSpeculatedOn = -1; // the oldest transaction that was speculated on
+       private long localArbitrationSequenceNumber = 0;
+       private boolean hadPartialSendToServer = false;
+       private boolean attemptedToSendToServer = false;
+       private long expectedsize;
+       private boolean didFindTableStatus = false;
+       private long currMaxSize = 0;
+
+       private Slot lastSlotAttemptedToSend = null;
+       private boolean lastIsNewKey = false;
+       private int lastNewSize = 0;
+       private Map<Transaction, List<Integer>> lastTransactionPartsSent = null;
+       private List<Entry> lastPendingSendArbitrationEntriesToDelete = null;
+       private NewKey lastNewKey = null;
+
+
+       /* Data Structures  */
+       private Map<IoTString, KeyValue> committedKeyValueTable = null; // Table of committed key value pairs
+       private Map<IoTString, KeyValue> speculatedKeyValueTable = null; // Table of speculated key value pairs, if there is a speculative value
+       private Map<IoTString, KeyValue> pendingTransactionSpeculatedKeyValueTable = null; // Table of speculated key value pairs, if there is a speculative value from the pending transactions
+       private Map<IoTString, NewKey> liveNewKeyTable = null; // Table of live new keys
+       private HashMap<Long, Pair<Long, Liveness>> lastMessageTable = null; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage);
+       private HashMap<Long, HashSet<RejectedMessage>> rejectedMessageWatchListTable = null; // Table of machine Ids and the set of rejected messages they have not seen yet
+       private Map<IoTString, Long> arbitratorTable = null; // Table of keys and their arbitrators
+       private Map<Pair<Long, Long>, Abort> liveAbortTable = null; // Table live abort messages
+       private Map<Long, Map<Pair<Long, Integer>, TransactionPart>> newTransactionParts = null; // transaction parts that are seen in this latest round of slots from the server
+       private Map<Long, Map<Pair<Long, Integer>, CommitPart>> newCommitParts = null; // commit parts that are seen in this latest round of slots from the server
+       private Map<Long, Long> lastArbitratedTransactionNumberByArbitratorTable = null; // Last transaction sequence number that an arbitrator arbitrated on
+       private Map<Long, Transaction> liveTransactionBySequenceNumberTable = null; // live transaction grouped by the sequence number
+       private Map<Pair<Long, Long>, Transaction> liveTransactionByTransactionIdTable = null; // live transaction grouped by the transaction ID
+       private Map<Long, Map<Long, Commit>> liveCommitsTable = null;
+       private Map<IoTString, Commit> liveCommitsByKeyTable = null;
+       private Map<Long, Long> lastCommitSeenSequenceNumberByArbitratorTable = null;
+       private Vector<Long> rejectedSlotList = null; // List of rejected slots that have yet to be sent to the server
+       private List<Transaction> pendingTransactionQueue = null;
+       private List<ArbitrationRound> pendingSendArbitrationRounds = null;
+       private List<Entry> pendingSendArbitrationEntriesToDelete = null;
+       private Map<Transaction, List<Integer>> transactionPartsSent = null;
+       private Map<Long, TransactionStatus> outstandingTransactionStatus = null;
+       private Map<Long, Abort> liveAbortsGeneratedByLocal = null;
+       private Set<Pair<Long, Long>> offlineTransactionsCommittedAndAtServer = null;
+       private Map<Long, Pair<String, Integer>> localCommunicationTable = null;
+       private Map<Long, Long> lastTransactionSeenFromMachineFromServer = null;
+       private Map<Long, Long> lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = null;
+
+
+       public Table(String baseurl, String password, long _localMachineId, int listeningPort) {
+               localMachineId = _localMachineId;
+               cloud = new CloudComm(this, baseurl, password, listeningPort);
+
+               init();
        }
 
-       public Table(CloudComm _cloud, long _localmachineid) {
-               localmachineid = _localmachineid;
-               buffer = new SlotBuffer();
-               numslots = buffer.capacity();
-               setResizeThreshold();
-               sequencenumber = 0;
+       public Table(CloudComm _cloud, long _localMachineId) {
+               localMachineId = _localMachineId;
                cloud = _cloud;
 
-               setupDataStructs();
+               init();
        }
 
-       private void setupDataStructs() {
-               pendingTransQueue = new LinkedList<PendingTransaction>();
-               commitMap = new HashMap<Long, Map<Long, Commit>>();
-               abortMap = new HashMap<Long, Abort>();
-               committedMapByKey = new HashMap<IoTString, Commit>();
-               commitedTable = new HashMap<IoTString, KeyValue>();
-               speculativeTable = new HashMap<IoTString, KeyValue>();
-               uncommittedTransactionsMap = new HashMap<Long, Transaction>();
-               arbitratorTable = new HashMap<IoTString, Long>();
-               newKeyTable = new HashMap<IoTString, NewKey>();
-               newCommitMap = new HashMap<Long, Map<Long, Commit>>();
-               lastCommitSeenSeqNumMap = new HashMap<Long, Long>();
-               lastCommitSeenTransSeqNumMap = new HashMap<Long, Long>();
-               lastAbortSeenSeqNumMap = new HashMap<Long, Long>();
-               pendingTransSpeculativeTable = new HashMap<IoTString, KeyValue>();
-               pendingCommitsList = new LinkedList<Commit>();
-               pendingCommitsToDelete = new LinkedList<Commit>();
-               localCommunicationChannels = new HashMap<Long, LocalComm>();
-               transactionStatusMap = new HashMap<Long, TransactionStatus>();
-       }
+       /**
+        * Init all the stuff needed for for table usage
+        */
+       private void init() {
 
-       public void initTable() throws ServerException {
-               cloud.setSalt();//Set the salt
-               Slot s = new Slot(this, 1, localmachineid);
-               TableStatus status = new TableStatus(s, numslots);
-               s.addEntry(status);
-               Slot[] array = cloud.putSlot(s, numslots);
-               if (array == null) {
-                       array = new Slot[] {s};
-                       /* update data structure */
-                       validateandupdate(array, true);
-               } else {
-                       throw new Error("Error on initialization");
-               }
-       }
+               // Init helper objects
+               random = new Random();
+               buffer = new SlotBuffer();
+
+               // Set Variables
+               oldestLiveSlotSequenceNumver = 1;
 
-       public void rebuild() throws ServerException {
-               Slot[] newslots = cloud.getSlots(sequencenumber + 1);
-               validateandupdate(newslots, true);
+               // init data structs
+               committedKeyValueTable = new HashMap<IoTString, KeyValue>();
+               speculatedKeyValueTable = new HashMap<IoTString, KeyValue>();
+               pendingTransactionSpeculatedKeyValueTable = new HashMap<IoTString, KeyValue>();
+               liveNewKeyTable = new HashMap<IoTString, NewKey>();
+               lastMessageTable = new HashMap<Long, Pair<Long, Liveness>>();
+               rejectedMessageWatchListTable = new HashMap<Long, HashSet<RejectedMessage>>();
+               arbitratorTable = new HashMap<IoTString, Long>();
+               liveAbortTable = new HashMap<Pair<Long, Long>, Abort>();
+               newTransactionParts = new HashMap<Long, Map<Pair<Long, Integer>, TransactionPart>>();
+               newCommitParts = new HashMap<Long, Map<Pair<Long, Integer>, CommitPart>>();
+               lastArbitratedTransactionNumberByArbitratorTable = new HashMap<Long, Long>();
+               liveTransactionBySequenceNumberTable = new HashMap<Long, Transaction>();
+               liveTransactionByTransactionIdTable = new HashMap<Pair<Long, Long>, Transaction>();
+               liveCommitsTable = new HashMap<Long, Map<Long, Commit>>();
+               liveCommitsByKeyTable = new HashMap<IoTString, Commit>();
+               lastCommitSeenSequenceNumberByArbitratorTable = new HashMap<Long, Long>();
+               rejectedSlotList = new Vector<Long>();
+               pendingTransactionQueue = new ArrayList<Transaction>();
+               pendingSendArbitrationEntriesToDelete = new ArrayList<Entry>();
+               transactionPartsSent = new HashMap<Transaction, List<Integer>>();
+               outstandingTransactionStatus = new HashMap<Long, TransactionStatus>();
+               liveAbortsGeneratedByLocal = new HashMap<Long, Abort>();
+               offlineTransactionsCommittedAndAtServer = new HashSet<Pair<Long, Long>>();
+               localCommunicationTable = new HashMap<Long, Pair<String, Integer>>();
+               lastTransactionSeenFromMachineFromServer = new HashMap<Long, Long>();
+               pendingSendArbitrationRounds = new ArrayList<ArbitrationRound>();
+               lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new HashMap<Long, Long>();
+
+
+               // Other init stuff
+               numberOfSlots = buffer.capacity();
+               setResizeThreshold();
        }
 
        // TODO: delete method
-       public void printSlots() {
+       public synchronized void printSlots() {
                long o = buffer.getOldestSeqNum();
                long n = buffer.getNewestSeqNum();
 
@@ -149,14 +168,34 @@ final public class Table {
 
                int livec = 0;
                int deadc = 0;
+
+               int casdasd = 0;
+
+               int liveslo = 0;
+
                for (long i = o; i < (n + 1); i++) {
                        Slot s = buffer.getSlot(i);
 
+
+                       if (s.isLive()) {
+                               liveslo++;
+                       }
+
                        Vector<Entry> entries = s.getEntries();
 
                        for (Entry e : entries) {
                                if (e.isLive()) {
                                        int type = e.getType();
+
+
+                                       if (type == 6) {
+                                               RejectedMessage rej = (RejectedMessage)e;
+                                               casdasd++;
+
+                                               System.out.println(rej.getMachineID());
+                                       }
+
+
                                        types[type] = types[type] + 1;
                                        num++;
                                        livec++;
@@ -170,83 +209,105 @@ final public class Table {
                        System.out.println(i + "    " + types[i]);
                }
                System.out.println("Live count:   " + livec);
+               System.out.println("Live Slot count:   " + liveslo);
+
                System.out.println("Dead count:   " + deadc);
                System.out.println("Old:   " + o);
                System.out.println("New:   " + n);
                System.out.println("Size:   " + buffer.size());
-               System.out.println("Commits Key Map:   " + commitedTable.size());
-               // System.out.println("Commits Live Map:   " + commitMap.size());
-               System.out.println("Pending:   " + pendingTransQueue.size());
-
-               // List<IoTString> strList = new ArrayList<IoTString>();
-               // for (int i = 0; i < 100; i++) {
-               //      String keyA = "a" + i;
-               //      String keyB = "b" + i;
-               //      String keyC = "c" + i;
-               //      String keyD = "d" + i;
-
-               //      IoTString iKeyA = new IoTString(keyA);
-               //      IoTString iKeyB = new IoTString(keyB);
-               //      IoTString iKeyC = new IoTString(keyC);
-               //      IoTString iKeyD = new IoTString(keyD);
-
-               //      strList.add(iKeyA);
-               //      strList.add(iKeyB);
-               //      strList.add(iKeyC);
-               //      strList.add(iKeyD);
-               // }
+               // System.out.println("Commits:   " + liveCommitsTable.size());
+               System.out.println("pendingTrans:   " + pendingTransactionQueue.size());
+               System.out.println("Trans Status Out:   " + outstandingTransactionStatus.size());
 
+               for (Long k : lastArbitratedTransactionNumberByArbitratorTable.keySet()) {
+                       System.out.println(k + ": " + lastArbitratedTransactionNumberByArbitratorTable.get(k));
+               }
 
-               // for (Long l : commitMap.keySet()) {
-               //      for (Long l2 : commitMap.get(l).keySet()) {
-               //              for (KeyValue kv : commitMap.get(l).get(l2).getkeyValueUpdateSet()) {
-               //                      strList.remove(kv.getKey());
-               //                      System.out.print(kv.getKey() + "    ");
-               //              }
-               //      }
-               // }
 
-               // System.out.println();
-               // System.out.println();
+               for (Long a : liveCommitsTable.keySet()) {
+                       for (Long b : liveCommitsTable.get(a).keySet()) {
+                               for (KeyValue kv : liveCommitsTable.get(a).get(b).getKeyValueUpdateSet()) {
+                                       System.out.print(kv + " ");
+                               }
+                               System.out.print("|| ");
+                       }
+                       System.out.println();
+               }
 
-               // for (IoTString s : strList) {
-               //      System.out.print(s + "    ");
-               // }
-               // System.out.println();
-               // System.out.println(strList.size());
        }
 
-       public long getId() {
-               return localmachineid;
+       /**
+        * Initialize the table by inserting a table status as the first entry into the table status
+        * also initialize the crypto stuff.
+        */
+       public synchronized void initTable() throws ServerException {
+               cloud.initSecurity();
+
+               // Create the first insertion into the block chain which is the table status
+               Slot s = new Slot(this, 1, localMachineId, localSequenceNumber);
+               localSequenceNumber++;
+               TableStatus status = new TableStatus(s, numberOfSlots);
+               s.addEntry(status);
+               Slot[] array = cloud.putSlot(s, numberOfSlots);
+
+               if (array == null) {
+                       array = new Slot[] {s};
+                       // update local block chain
+                       validateAndUpdate(array, true);
+               } else if (array.length == 1) {
+                       // in case we did push the slot BUT we failed to init it
+                       validateAndUpdate(array, true);
+               } else {
+                       throw new Error("Error on initialization");
+               }
        }
 
-       public boolean hasConnection() {
-               return cloud.hasConnection();
+       /**
+        * Rebuild the table from scratch by pulling the latest block chain from the server.
+        */
+       public synchronized void rebuild() throws ServerException {
+               // Just pull the latest slots from the server
+               Slot[] newslots = cloud.getSlots(sequenceNumber + 1);
+               validateAndUpdate(newslots, true);
+               sendToServer(null);
+               updateLiveTransactionsAndStatus();
+
        }
 
-       public String toString() {
-               String retString = " Committed Table: \n";
-               retString += "---------------------------\n";
-               retString += commitedTable.toString();
+       // public String toString() {
+       //      String retString = " Committed Table: \n";
+       //      retString += "---------------------------\n";
+       //      retString += commitedTable.toString();
 
-               retString += "\n\n";
+       //      retString += "\n\n";
 
-               retString += " Speculative Table: \n";
-               retString += "---------------------------\n";
-               retString += speculativeTable.toString();
+       //      retString += " Speculative Table: \n";
+       //      retString += "---------------------------\n";
+       //      retString += speculativeTable.toString();
 
-               return retString;
-       }
+       //      return retString;
+       // }
 
-       public void addLocalComm(long machineId, LocalComm lc) {
-               localCommunicationChannels.put(machineId, lc);
+       public synchronized void addLocalCommunication(long arbitrator, String hostName, int portNumber) {
+               localCommunicationTable.put(arbitrator, new Pair<String, Integer>(hostName, portNumber));
        }
-       public Long getArbitrator(IoTString key) {
+
+       public synchronized Long getArbitrator(IoTString key) {
                return arbitratorTable.get(key);
        }
 
-       public IoTString getCommitted(IoTString key) {
-               KeyValue kv = commitedTable.get(key);
+       public synchronized void close() {
+               cloud.close();
+       }
+       
+       // Return all keys in the table
+       public synchronized Set<IoTString> getKeys() {
+               return committedKeyValueTable.keySet();
+       }
+
+       public synchronized IoTString getCommitted(IoTString key)  {
+               KeyValue kv = committedKeyValueTable.get(key);
+
                if (kv != null) {
                        return kv.getValue();
                } else {
@@ -254,15 +315,15 @@ final public class Table {
                }
        }
 
-       public IoTString getSpeculative(IoTString key) {
-               KeyValue kv = pendingTransSpeculativeTable.get(key);
+       public synchronized IoTString getSpeculative(IoTString key) {
+               KeyValue kv = pendingTransactionSpeculatedKeyValueTable.get(key);
 
                if (kv == null) {
-                       kv = speculativeTable.get(key);
+                       kv = speculatedKeyValueTable.get(key);
                }
 
                if (kv == null) {
-                       kv = commitedTable.get(key);
+                       kv = committedKeyValueTable.get(key);
                }
 
                if (kv != null) {
@@ -272,583 +333,1035 @@ final public class Table {
                }
        }
 
-       public IoTString getCommittedAtomic(IoTString key) {
-               KeyValue kv = commitedTable.get(key);
+       public synchronized IoTString getCommittedAtomic(IoTString key) {
+               KeyValue kv = committedKeyValueTable.get(key);
 
                if (arbitratorTable.get(key) == null) {
                        throw new Error("Key not Found.");
                }
 
                // Make sure new key value pair matches the current arbitrator
-               if (!pendingTransBuild.checkArbitrator(arbitratorTable.get(key))) {
+               if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
                        // TODO: Maybe not throw en error
                        throw new Error("Not all Key Values Match Arbitrator.");
                }
 
                if (kv != null) {
-                       pendingTransBuild.addKVGuard(new KeyValue(key, kv.getValue()));
+                       pendingTransactionBuilder.addKVGuard(new KeyValue(key, kv.getValue()));
                        return kv.getValue();
                } else {
-                       pendingTransBuild.addKVGuard(new KeyValue(key, null));
+                       pendingTransactionBuilder.addKVGuard(new KeyValue(key, null));
                        return null;
                }
        }
 
-       public IoTString getSpeculativeAtomic(IoTString key) {
-
+       public synchronized IoTString getSpeculativeAtomic(IoTString key) {
                if (arbitratorTable.get(key) == null) {
                        throw new Error("Key not Found.");
                }
 
                // Make sure new key value pair matches the current arbitrator
-               if (!pendingTransBuild.checkArbitrator(arbitratorTable.get(key))) {
+               if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
                        // TODO: Maybe not throw en error
                        throw new Error("Not all Key Values Match Arbitrator.");
                }
 
-               KeyValue kv = pendingTransSpeculativeTable.get(key);
+               KeyValue kv = pendingTransactionSpeculatedKeyValueTable.get(key);
 
                if (kv == null) {
-                       kv = speculativeTable.get(key);
+                       kv = speculatedKeyValueTable.get(key);
                }
 
                if (kv == null) {
-                       kv = commitedTable.get(key);
+                       kv = committedKeyValueTable.get(key);
                }
 
                if (kv != null) {
-                       pendingTransBuild.addKVGuard(new KeyValue(key, kv.getValue()));
+                       pendingTransactionBuilder.addKVGuard(new KeyValue(key, kv.getValue()));
                        return kv.getValue();
                } else {
-                       pendingTransBuild.addKVGuard(new KeyValue(key, null));
+                       pendingTransactionBuilder.addKVGuard(new KeyValue(key, null));
                        return null;
                }
        }
 
-       public void update() {
+       public synchronized boolean update()  {
                try {
-                       Slot[] newslots = cloud.getSlots(sequencenumber + 1);
-                       validateandupdate(newslots, false);
+                       Slot[] newSlots = cloud.getSlots(sequenceNumber + 1);
+                       validateAndUpdate(newSlots, false);
+                       sendToServer(null);
 
-                       if (!pendingTransQueue.isEmpty()) {
 
-                               // We have a pending transaction so do full insertion
-                               processPendingTrans();
-                       } else {
+                       updateLiveTransactionsAndStatus();
 
-                               // We dont have a pending transaction so do minimal effort
-                               updateWithNotPendingTrans();
+                       return true;
+               } catch (Exception e) {
+                       // e.printStackTrace();
+
+                       for (Long m : localCommunicationTable.keySet()) {
+                               updateFromLocal(m);
                        }
+               }
 
-               } catch (Exception e) {
-                       // could not update so do nothing
+               return false;
+       }
+
+       public synchronized boolean createNewKey(IoTString keyName, long machineId) throws ServerException {
+               while (true) {
+                       if (arbitratorTable.get(keyName) != null) {
+                               // There is already an arbitrator
+                               return false;
+                       }
+
+                       NewKey newKey = new NewKey(null, keyName, machineId);
+
+                       if (sendToServer(newKey)) {
+                               // If successfully inserted
+                               return true;
+                       }
                }
        }
 
-       public void startTransaction() {
+       public synchronized void startTransaction() {
                // Create a new transaction, invalidates any old pending transactions.
-               pendingTransBuild = new PendingTransaction();
+               pendingTransactionBuilder = new PendingTransaction(localMachineId);
        }
 
-       public void addKV(IoTString key, IoTString value) {
+       public synchronized void addKV(IoTString key, IoTString value) {
 
+               // Make sure it is a valid key
                if (arbitratorTable.get(key) == null) {
                        throw new Error("Key not Found.");
                }
 
                // Make sure new key value pair matches the current arbitrator
-               if (!pendingTransBuild.checkArbitrator(arbitratorTable.get(key))) {
+               if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
                        // TODO: Maybe not throw en error
                        throw new Error("Not all Key Values Match Arbitrator.");
                }
 
+               // Add the key value to this transaction
                KeyValue kv = new KeyValue(key, value);
-               pendingTransBuild.addKV(kv);
+               pendingTransactionBuilder.addKV(kv);
        }
 
-       public TransactionStatus commitTransaction() {
-
-               if (pendingTransBuild.getKVUpdates().size() == 0) {
+       public synchronized TransactionStatus commitTransaction() {
 
+               if (pendingTransactionBuilder.getKVUpdates().size() == 0) {
                        // transaction with no updates will have no effect on the system
                        return new TransactionStatus(TransactionStatus.StatusNoEffect, -1);
                }
 
-               TransactionStatus transStatus = null;
+               // Set the local transaction sequence number and increment
+               pendingTransactionBuilder.setClientLocalSequenceNumber(localTransactionSequenceNumber);
+               localTransactionSequenceNumber++;
+
+               // Create the transaction status
+               TransactionStatus transactionStatus = new TransactionStatus(TransactionStatus.StatusPending, pendingTransactionBuilder.getArbitrator());
 
-               if (pendingTransBuild.getArbitrator() != localmachineid) {
+               // Create the new transaction
+               Transaction newTransaction = pendingTransactionBuilder.createTransaction();
+               newTransaction.setTransactionStatus(transactionStatus);
 
-                       // set the local sequence number so we can recognize this transaction later
-                       pendingTransBuild.setMachineLocalTransSeqNum(localTransactionSequenceNumber);
-                       localTransactionSequenceNumber++;
+               if (pendingTransactionBuilder.getArbitrator() != localMachineId) {
+                       // Add it to the queue and invalidate the builder for safety
+                       pendingTransactionQueue.add(newTransaction);
+               } else {
+                       arbitrateOnLocalTransaction(newTransaction);
+                       updateLiveStateFromLocal();
+               }
 
-                       transStatus = new TransactionStatus(TransactionStatus.StatusPending, pendingTransBuild.getArbitrator());
-                       transactionStatusMap.put(pendingTransBuild.getMachineLocalTransSeqNum(), transStatus);
+               pendingTransactionBuilder = new PendingTransaction(localMachineId);
 
-                       // Add the pending transaction to the queue
-                       pendingTransQueue.add(pendingTransBuild);
+               try {
+                       sendToServer(null);
+               } catch (ServerException e) {
 
+                       Set<Long> arbitratorTriedAndFailed = new HashSet<Long>();
+                       for (Iterator<Transaction> iter = pendingTransactionQueue.iterator(); iter.hasNext(); ) {
+                               Transaction transaction = iter.next();
 
-                       for (int i = lastSeenPendingTransactionSpeculateIndex; i < pendingTransQueue.size(); i++) {
-                               PendingTransaction pt = pendingTransQueue.get(i);
+                               if (arbitratorTriedAndFailed.contains(transaction.getArbitrator())) {
+                                       // Already contacted this client so ignore all attempts to contact this client
+                                       // to preserve ordering for arbitrator
+                                       continue;
+                               }
 
-                               if (pt.evaluateGuard(commitedTable, speculativeTable, pendingTransSpeculativeTable)) {
+                               Pair<Boolean, Boolean> sendReturn = sendTransactionToLocal(transaction);
 
-                                       lastSeenPendingTransactionSpeculateIndex = i;
+                               if (sendReturn.getFirst()) {
+                                       // Failed to contact over local
+                                       arbitratorTriedAndFailed.add(transaction.getArbitrator());
+                               } else {
+                                       // Successful contact or should not contact
 
-                                       for (KeyValue kv : pt.getKVUpdates()) {
-                                               pendingTransSpeculativeTable.put(kv.getKey(), kv);
+                                       if (sendReturn.getSecond()) {
+                                               // did arbitrate
+                                               iter.remove();
                                        }
-
                                }
                        }
-               } else {
-                       Transaction ut = new Transaction(null,
-                                                        -1,
-                                                        localmachineid,
-                                                        pendingTransBuild.getArbitrator(),
-                                                        pendingTransBuild.getKVUpdates(),
-                                                        pendingTransBuild.getKVGuard());
-
-                       Pair<Boolean, List<Commit>> retData = doLocalUpdateAndArbitrate(ut, lastCommitSeenSeqNumMap.get(localmachineid));
-
-                       if (retData.getFirst()) {
-                               transStatus = new TransactionStatus(TransactionStatus.StatusCommitted, pendingTransBuild.getArbitrator());
-                       } else {
-                               transStatus = new TransactionStatus(TransactionStatus.StatusAborted, pendingTransBuild.getArbitrator());
-                       }
                }
 
-               // Try to insert transactions if possible
-               if (!pendingTransQueue.isEmpty()) {
-                       // We have a pending transaction so do full insertion
-                       processPendingTrans();
-               } else {
-                       try {
-                               // We dont have a pending transaction so do minimal effort
-                               updateWithNotPendingTrans();
-                       } catch (Exception e) {
-                               // Do nothing
-                       }
-               }
+               updateLiveStateFromLocal();
 
-               // reset it so next time is fresh
-               pendingTransBuild = new PendingTransaction();
+               return transactionStatus;
+       }
 
-               return transStatus;
+       /**
+        * Get the machine ID for this client
+        */
+       public long getMachineId() {
+               return localMachineId;
        }
 
-       public boolean createNewKey(IoTString keyName, long machineId) throws ServerException {
+       /**
+        * Decrement the number of live slots that we currently have
+        */
+       public void decrementLiveCount() {
+               liveSlotCount--;
+       }
 
-               while (true) {
-                       if (arbitratorTable.get(keyName) != null) {
-                               // There is already an arbitrator
-                               return false;
-                       }
+       /**
+        * Recalculate the new resize threshold
+        */
+       private void setResizeThreshold() {
+               int resizeLower = (int) (RESIZE_THRESHOLD * numberOfSlots);
+               bufferResizeThreshold = resizeLower - 1 + random.nextInt(numberOfSlots - resizeLower);
+       }
 
-                       if (tryput(keyName, machineId, false)) {
-                               // If successfully inserted
-                               return true;
-                       }
-               }
+       public long getLocalSequenceNumber() {
+               return localSequenceNumber;
        }
 
-       private void processPendingTrans() {
 
-               boolean sentAllPending = false;
+       boolean lastInsertedNewKey = false;
+
+       private boolean sendToServer(NewKey newKey) throws ServerException {
+
+               boolean fromRetry = false;
+
                try {
-                       while (!pendingTransQueue.isEmpty()) {
-                               if (tryput( pendingTransQueue.peek(), false)) {
-                                       pendingTransQueue.poll();
-                               }
-                       }
+                       if (hadPartialSendToServer) {
+                               Slot[] newSlots = cloud.getSlots(sequenceNumber + 1);
+                               if (newSlots.length == 0) {
+                                       fromRetry = true;
+                                       ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey);
+
+                                       if (sendSlotsReturn.getFirst()) {
+                                               if (newKey != null) {
+                                                       if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
+                                                               newKey = null;
+                                                       }
+                                               }
 
-                       // if got here then all pending transactions were sent
-                       sentAllPending = true;
-               } catch (Exception e) {
-                       // There was a connection error
-                       e.printStackTrace();
-                       sentAllPending = false;
-               }
+                                               for (Transaction transaction : lastTransactionPartsSent.keySet()) {
+                                                       transaction.resetServerFailure();
 
+                                                       // Update which transactions parts still need to be sent
+                                                       transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
 
-               if (!sentAllPending) {
+                                                       // Add the transaction status to the outstanding list
+                                                       outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
 
-                       for (Iterator<PendingTransaction> i = pendingTransQueue.iterator(); i.hasNext(); ) {
-                               PendingTransaction pt = i.next();
-                               LocalComm lc = localCommunicationChannels.get(pt.getArbitrator());
-                               if (lc == null) {
-                                       // Cant talk directly to arbitrator so cant do anything
-                                       continue;
-                               }
+                                                       // Update the transaction status
+                                                       transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
 
+                                                       // Check if all the transaction parts were successfully sent and if so then remove it from pending
+                                                       if (transaction.didSendAllParts()) {
+                                                               transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
+                                                               pendingTransactionQueue.remove(transaction);
+                                                       }
+                                               }
+                                       } else {
 
-                               Transaction ut = new Transaction(null,
-                                                                -1,
-                                                                localmachineid,
-                                                                pendingTransBuild.getArbitrator(),
-                                                                pendingTransBuild.getKVUpdates(),
-                                                                pendingTransBuild.getKVGuard());
+                                               newSlots = sendSlotsReturn.getThird();
 
+                                               boolean isInserted = false;
+                                               for (Slot s : newSlots) {
+                                                       if ((s.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
+                                                               isInserted = true;
+                                                               break;
+                                                       }
+                                               }
 
-                               Pair<Boolean, List<Commit>> retData = sendTransactionToLocal(ut, lc);
+                                               for (Slot s : newSlots) {
+                                                       if (isInserted) {
+                                                               break;
+                                                       }
+
+                                                       // Process each entry in the slot
+                                                       for (Entry entry : s.getEntries()) {
+
+                                                               if (entry.getType() == Entry.TypeLastMessage) {
+                                                                       LastMessage lastMessage = (LastMessage)entry;
+                                                                       if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber())) {
+                                                                               isInserted = true;
+                                                                               break;
+                                                                       }
+                                                               }
+                                                       }
+                                               }
 
-                               for (Commit commit : retData.getSecond()) {
-                                       // Prepare to process the commit
-                                       processEntry(commit);
-                               }
+                                               if (isInserted) {
+                                                       if (newKey != null) {
+                                                               if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
+                                                                       newKey = null;
+                                                               }
+                                                       }
+
+                                                       for (Transaction transaction : lastTransactionPartsSent.keySet()) {
+                                                               transaction.resetServerFailure();
+
+                                                               // Update which transactions parts still need to be sent
+                                                               transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
+
+                                                               // Add the transaction status to the outstanding list
+                                                               outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
+
+                                                               // Update the transaction status
+                                                               transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
+
+                                                               // Check if all the transaction parts were successfully sent and if so then remove it from pending
+                                                               if (transaction.didSendAllParts()) {
+                                                                       transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
+                                                                       pendingTransactionQueue.remove(transaction);
+                                                               } else {
+                                                                       transaction.resetServerFailure();
+                                                                       // Set the transaction sequence number back to nothing
+                                                                       if (!transaction.didSendAPartToServer()) {
+                                                                               transaction.setSequenceNumber(-1);
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
 
-                               boolean didCommitOrSpeculate = proccessAllNewCommits();
+                                       for (Transaction transaction : lastTransactionPartsSent.keySet()) {
+                                               transaction.resetServerFailure();
+                                               // Set the transaction sequence number back to nothing
+                                               if (!transaction.didSendAPartToServer()) {
+                                                       transaction.setSequenceNumber(-1);
+                                               }
+                                       }
 
-                               // Go through all uncommitted transactions and kill the ones that are dead
-                               deleteDeadUncommittedTransactions();
+                                       if (sendSlotsReturn.getThird().length != 0) {
+                                               // insert into the local block chain
+                                               validateAndUpdate(sendSlotsReturn.getThird(), true);
+                                       }
+                                       // continue;
+                               } else {
+                                       boolean isInserted = false;
+                                       for (Slot s : newSlots) {
+                                               if ((s.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
+                                                       isInserted = true;
+                                                       break;
+                                               }
+                                       }
 
-                               // Speculate on key value pairs
-                               didCommitOrSpeculate |= createSpeculativeTable();
-                               createPendingTransactionSpeculativeTable(didCommitOrSpeculate);
+                                       for (Slot s : newSlots) {
+                                               if (isInserted) {
+                                                       break;
+                                               }
 
+                                               // Process each entry in the slot
+                                               for (Entry entry : s.getEntries()) {
 
-                               if (retData.getFirst()) {
-                                       TransactionStatus transStatus = transactionStatusMap.remove(pendingTransBuild.getMachineLocalTransSeqNum());
-                                       if (transStatus != null) {
-                                               transStatus.setStatus(TransactionStatus.StatusCommitted);
+                                                       if (entry.getType() == Entry.TypeLastMessage) {
+                                                               LastMessage lastMessage = (LastMessage)entry;
+                                                               if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber())) {
+                                                                       isInserted = true;
+                                                                       break;
+                                                               }
+                                                       }
+                                               }
                                        }
 
-                               } else {
-                                       TransactionStatus transStatus = transactionStatusMap.remove(pendingTransBuild.getMachineLocalTransSeqNum());
-                                       if (transStatus != null) {
-                                               transStatus.setStatus(TransactionStatus.StatusAborted);
+                                       if (isInserted) {
+                                               if (newKey != null) {
+                                                       if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
+                                                               newKey = null;
+                                                       }
+                                               }
+
+                                               for (Transaction transaction : lastTransactionPartsSent.keySet()) {
+                                                       transaction.resetServerFailure();
+
+                                                       // Update which transactions parts still need to be sent
+                                                       transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
+
+                                                       // Add the transaction status to the outstanding list
+                                                       outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
+
+                                                       // Update the transaction status
+                                                       transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
+
+                                                       // Check if all the transaction parts were successfully sent and if so then remove it from pending
+                                                       if (transaction.didSendAllParts()) {
+                                                               transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
+                                                               pendingTransactionQueue.remove(transaction);
+                                                       } else {
+                                                               transaction.resetServerFailure();
+                                                               // Set the transaction sequence number back to nothing
+                                                               if (!transaction.didSendAPartToServer()) {
+                                                                       transaction.setSequenceNumber(-1);
+                                                               }
+                                                       }
+                                               }
+                                       } else {
+                                               for (Transaction transaction : lastTransactionPartsSent.keySet()) {
+                                                       transaction.resetServerFailure();
+                                                       // Set the transaction sequence number back to nothing
+                                                       if (!transaction.didSendAPartToServer()) {
+                                                               transaction.setSequenceNumber(-1);
+                                                       }
+                                               }
                                        }
+
+                                       // insert into the local block chain
+                                       validateAndUpdate(newSlots, true);
                                }
                        }
+               } catch (ServerException e) {
+                       throw e;
                }
-       }
 
-       private void updateWithNotPendingTrans() throws ServerException {
 
-               boolean doEnd = false;
-               boolean needResize = false;
-               while (!doEnd && ((uncommittedTransactionsMap.keySet().size() > 0)  || (pendingCommitsList.size() > 0))   ) {
-                       boolean resize = needResize;
-                       needResize = false;
 
-                       Slot s = new Slot(this, sequencenumber + 1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
-                       int newsize = 0;
-                       if (liveslotcount > resizethreshold) {
-                               resize = true; //Resize is forced
-                       }
+               try {
+                       // While we have stuff that needs inserting into the block chain
+                       while ((pendingTransactionQueue.size() > 0) || (pendingSendArbitrationRounds.size() > 0) || (newKey != null)) {
 
-                       if (resize) {
-                               newsize = (int) (numslots * RESIZE_MULTIPLE);
-                               TableStatus status = new TableStatus(s, newsize);
-                               s.addEntry(status);
-                       }
+                               fromRetry = false;
 
-                       doRejectedMessages(s);
+                               if (hadPartialSendToServer) {
+                                       throw new Error("Should Be error free");
+                               }
 
-                       ThreeTuple<Boolean, Boolean, Long> retTup = doMandatoryResuce(s, resize);
 
-                       // Resize was needed so redo call
-                       if (retTup.getFirst()) {
-                               needResize = true;
-                               continue;
-                       }
 
-                       // Extract working variables
-                       boolean seenliveslot = retTup.getSecond();
-                       long seqn = retTup.getThird();
+                               // If there is a new key with same name then end
+                               if ((newKey != null) && (arbitratorTable.get(newKey.getKey()) != null)) {
+                                       return false;
+                               }
 
-                       // Did need to arbitrate
-                       doEnd = !doArbitration(s);
+                               // Create the slot
+                               Slot slot = new Slot(this, sequenceNumber + 1, localMachineId, buffer.getSlot(sequenceNumber).getHMAC(), localSequenceNumber);
+                               localSequenceNumber++;
 
-                       doOptionalRescue(s, seenliveslot, seqn, resize);
+                               // Try to fill the slot with data
+                               ThreeTuple<Boolean, Integer, Boolean> fillSlotsReturn = fillSlot(slot, false, newKey);
+                               boolean needsResize = fillSlotsReturn.getFirst();
+                               int newSize = fillSlotsReturn.getSecond();
+                               Boolean insertedNewKey = fillSlotsReturn.getThird();
 
-                       int max = 0;
-                       if (resize) {
-                               max = newsize;
-                       }
+                               if (needsResize) {
+                                       // Reset which transaction to send
+                                       for (Transaction transaction : transactionPartsSent.keySet()) {
+                                               transaction.resetNextPartToSend();
 
-                       Slot[] array = cloud.putSlot(s, max);
-                       if (array == null) {
-                               array = new Slot[] {s};
-                               rejectedmessagelist.clear();
+                                               // Set the transaction sequence number back to nothing
+                                               if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
+                                                       transaction.setSequenceNumber(-1);
+                                               }
+                                       }
 
-                               // Delete pending commits that were sent to the cloud
-                               deletePendingCommits();
+                                       // Clear the sent data since we are trying again
+                                       pendingSendArbitrationEntriesToDelete.clear();
+                                       transactionPartsSent.clear();
 
-                       }       else {
-                               if (array.length == 0)
-                                       throw new Error("Server Error: Did not send any slots");
-                               rejectedmessagelist.add(s.getSequenceNumber());
-                               doEnd = false;
-                       }
+                                       // We needed a resize so try again
+                                       fillSlot(slot, true, newKey);
+                               }
 
-                       /* update data structure */
-                       validateandupdate(array, true);
-               }
-       }
+                               lastSlotAttemptedToSend = slot;
+                               lastIsNewKey = (newKey != null);
+                               lastInsertedNewKey = insertedNewKey;
+                               lastNewSize = newSize;
+                               lastNewKey = newKey;
+                               lastTransactionPartsSent = new HashMap<Transaction, List<Integer>>(transactionPartsSent);
+                               lastPendingSendArbitrationEntriesToDelete = new ArrayList<Entry>(pendingSendArbitrationEntriesToDelete);
 
-       private Pair<Boolean, List<Commit>> sendTransactionToLocal(Transaction ut, LocalComm lc) {
 
-               // encode the request
-               byte[] array = new byte[Long.BYTES + ut.getSize()];
-               ByteBuffer bbEncode = ByteBuffer.wrap(array);
-               Long lastSeenCommit = lastCommitSeenSeqNumMap.get(ut.getArbitrator());
-               if (lastSeenCommit != null) {
-                       bbEncode.putLong(lastSeenCommit);
-               } else {
-                       bbEncode.putLong(0);
-               }
-               ut.encode(bbEncode);
+                               ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != null);
 
-               byte[] data = lc.sendDataToLocalDevice(ut.getArbitrator(), bbEncode.array());
+                               if (sendSlotsReturn.getFirst()) {
 
-               // Decode the data
-               ByteBuffer bbDecode = ByteBuffer.wrap(data);
-               boolean didCommit = bbDecode.get() == 1;
-               int numberOfCommites = bbDecode.getInt();
+                                       // Did insert into the block chain
 
-               List<Commit> newCommits = new LinkedList<Commit>();
-               for (int i = 0; i < numberOfCommites; i++ ) {
-                       bbDecode.get();
-                       Commit com = (Commit)Commit.decode(null, bbDecode);
-                       newCommits.add(com);
-               }
+                                       if (insertedNewKey) {
+                                               // This slot was what was inserted not a previous slot
 
-               return new Pair<Boolean, List<Commit>>(didCommit, newCommits);
-       }
+                                               // New Key was successfully inserted into the block chain so dont want to insert it again
+                                               newKey = null;
+                                       }
 
-       public byte[] localCommInput(byte[] data) {
+                                       // Remove the aborts and commit parts that were sent from the pending to send queue
+                                       for (Iterator<ArbitrationRound> iter = pendingSendArbitrationRounds.iterator(); iter.hasNext(); ) {
+                                               ArbitrationRound round = iter.next();
+                                               round.removeParts(pendingSendArbitrationEntriesToDelete);
 
-               // Decode the data
-               ByteBuffer bbDecode = ByteBuffer.wrap(data);
-               long lastSeenCommit = bbDecode.getLong();
-               bbDecode.get();
-               Transaction ut = (Transaction)Transaction.decode(null, bbDecode);
+                                               if (round.isDoneSending()) {
+                                                       // Sent all the parts
+                                                       iter.remove();
+                                               }
+                                       }
 
-               // Do the local update and arbitrate
-               Pair<Boolean, List<Commit>> returnData = doLocalUpdateAndArbitrate(ut, lastSeenCommit);
+                                       for (Transaction transaction : transactionPartsSent.keySet()) {
+                                               transaction.resetServerFailure();
 
-               // Calculate the size of the response
-               int size = Byte.BYTES + Integer.BYTES;
-               for (Commit com : returnData.getSecond()) {
-                       size += com.getSize();
-               }
+                                               // Update which transactions parts still need to be sent
+                                               transaction.removeSentParts(transactionPartsSent.get(transaction));
 
-               // encode the response
-               byte[] array = new byte[size];
-               ByteBuffer bbEncode = ByteBuffer.wrap(array);
-               if (returnData.getFirst()) {
-                       bbEncode.put((byte)1);
-               } else {
-                       bbEncode.put((byte)0);
-               }
-               bbEncode.putInt(returnData.getSecond().size());
+                                               // Add the transaction status to the outstanding list
+                                               outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
 
-               for (Commit com : returnData.getSecond()) {
-                       com.encode(bbEncode);
-               }
+                                               // Update the transaction status
+                                               transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
 
-               return bbEncode.array();
-       }
+                                               // Check if all the transaction parts were successfully sent and if so then remove it from pending
+                                               if (transaction.didSendAllParts()) {
+                                                       transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
+                                                       pendingTransactionQueue.remove(transaction);
+                                               }
+                                       }
+                               } else {
 
-       private Pair<Boolean, List<Commit>> doLocalUpdateAndArbitrate(Transaction ut, Long lastCommitSeen) {
+                                       // if (!sendSlotsReturn.getSecond()) {
+                                       //      for (Transaction transaction : lastTransactionPartsSent.keySet()) {
+                                       //              transaction.resetServerFailure();
+                                       //      }
+                                       // } else {
+                                       //      for (Transaction transaction : lastTransactionPartsSent.keySet()) {
+                                       //              transaction.resetServerFailure();
+
+                                       //              // Update which transactions parts still need to be sent
+                                       //              transaction.removeSentParts(transactionPartsSent.get(transaction));
+
+                                       //              // Add the transaction status to the outstanding list
+                                       //              outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
+
+                                       //              // Update the transaction status
+                                       //              transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
+
+                                       //              // Check if all the transaction parts were successfully sent and if so then remove it from pending
+                                       //              if (transaction.didSendAllParts()) {
+                                       //                      transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
+                                       //                      pendingTransactionQueue.remove(transaction);
+
+                                       //                      for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
+                                       //                              System.out.println("Sent: " + kv + "  from: " + localMachineId + "   Slot:" + lastSlotAttemptedToSend.getSequenceNumber() + "  Claimed:" + transaction.getSequenceNumber());
+                                       //                      }
+                                       //              }
+                                       //      }
+                                       // }
+
+                                       // Reset which transaction to send
+                                       for (Transaction transaction : transactionPartsSent.keySet()) {
+                                               transaction.resetNextPartToSend();
+                                               // transaction.resetNextPartToSend();
+
+                                               // Set the transaction sequence number back to nothing
+                                               if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
+                                                       transaction.setSequenceNumber(-1);
+                                               }
+                                       }
+                               }
 
-               if (ut.getArbitrator() != localmachineid) {
-                       // We are not the arbitrator for that transaction so the other device is talking to the wrong arbitrator
-                       return null;
-               }
+                               // Clear the sent data in preparation for next send
+                               pendingSendArbitrationEntriesToDelete.clear();
+                               transactionPartsSent.clear();
 
-               List<Commit> returnCommits = new ArrayList<Commit>();
+                               if (sendSlotsReturn.getThird().length != 0) {
+                                       // insert into the local block chain
+                                       validateAndUpdate(sendSlotsReturn.getThird(), true);
+                               }
+                       }
 
-               if ((lastCommitSeenSeqNumMap.get(localmachineid) != null) && (lastCommitSeenSeqNumMap.get(localmachineid) > lastCommitSeen)) {
-                       // There is a commit that the other client has not seen yet
+               } catch (ServerException e) {
 
-                       Map<Long, Commit> cm = commitMap.get(localmachineid);
-                       if (cm != null) {
+                       if (e.getType() != ServerException.TypeInputTimeout) {
+                               // e.printStackTrace();
 
-                               List<Long> commitKeys = new ArrayList<Long>(cm.keySet());
-                               Collections.sort(commitKeys);
+                               // Nothing was able to be sent to the server so just clear these data structures
+                               for (Transaction transaction : transactionPartsSent.keySet()) {
+                                       transaction.resetNextPartToSend();
 
+                                       // Set the transaction sequence number back to nothing
+                                       if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
+                                               transaction.setSequenceNumber(-1);
+                                       }
+                               }
+                       } else {
+                               // There was a partial send to the server
+                               hadPartialSendToServer = true;
 
-                               for (int i = (commitKeys.size() - 1); i >= 0; i--) {
-                                       Commit com = cm.get(commitKeys.get(i));
 
-                                       if (com.getSequenceNumber() <= lastCommitSeen) {
-                                               break;
-                                       }
-                                       returnCommits.add((Commit)com.getCopy(null));
+                               // if (!fromRetry) {
+                               //      lastTransactionPartsSent = new HashMap<Transaction, List<Integer>>(transactionPartsSent);
+                               //      lastPendingSendArbitrationEntriesToDelete = new ArrayList<Entry>(pendingSendArbitrationEntriesToDelete);
+                               // }
+
+                               // Nothing was able to be sent to the server so just clear these data structures
+                               for (Transaction transaction : transactionPartsSent.keySet()) {
+                                       transaction.resetNextPartToSend();
+                                       transaction.setServerFailure();
                                }
                        }
+
+                       pendingSendArbitrationEntriesToDelete.clear();
+                       transactionPartsSent.clear();
+
+                       throw e;
                }
 
-               if (!ut.evaluateGuard(commitedTable, null)) {
-                       // Guard evaluated as false so return only the commits that the other device has not seen yet
-                       return new Pair<Boolean, List<Commit>>(false, returnCommits);
+               return newKey == null;
+       }
+
+       private synchronized boolean updateFromLocal(long machineId) {
+               Pair<String, Integer> localCommunicationInformation = localCommunicationTable.get(machineId);
+               if (localCommunicationInformation == null) {
+                       // Cant talk to that device locally so do nothing
+                       return false;
                }
 
-               // create the commit
-               Commit commit = new Commit(null,
-                                          -1,
-                                          commitSequenceNumber,
-                                          ut.getArbitrator(),
-                                          ut.getkeyValueUpdateSet());
-               commitSequenceNumber = commitSequenceNumber + 1;
+               // Get the size of the send data
+               int sendDataSize = Integer.BYTES + Long.BYTES;
 
-               // Add to the pending commits list
-               pendingCommitsList.add(commit);
+               Long lastArbitrationDataLocalSequenceNumber = (long) - 1;
+               if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(machineId) != null) {
+                       lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(machineId);
+               }
 
-               // Add this commit so we can send it back
-               returnCommits.add(commit);
+               byte[] sendData = new byte[sendDataSize];
+               ByteBuffer bbEncode = ByteBuffer.wrap(sendData);
 
-               // Prepare to process the commit
-               processEntry(commit);
+               // Encode the data
+               bbEncode.putLong(lastArbitrationDataLocalSequenceNumber);
+               bbEncode.putInt(0);
 
-               boolean didCommitOrSpeculate = proccessAllNewCommits();
+               // Send by local
+               byte[] returnData = cloud.sendLocalData(sendData, localSequenceNumber, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
+               localSequenceNumber++;
 
-               // Go through all uncommitted transactions and kill the ones that are dead
-               deleteDeadUncommittedTransactions();
+               if (returnData == null) {
+                       // Could not contact server
+                       return false;
+               }
 
-               // Speculate on key value pairs
-               didCommitOrSpeculate |= createSpeculativeTable();
-               createPendingTransactionSpeculativeTable(didCommitOrSpeculate);
+               // Decode the data
+               ByteBuffer bbDecode = ByteBuffer.wrap(returnData);
+               int numberOfEntries = bbDecode.getInt();
+
+               for (int i = 0; i < numberOfEntries; i++) {
+                       byte type = bbDecode.get();
+                       if (type == Entry.TypeAbort) {
+                               Abort abort = (Abort)Abort.decode(null, bbDecode);
+                               processEntry(abort);
+                       } else if (type == Entry.TypeCommitPart) {
+                               CommitPart commitPart = (CommitPart)CommitPart.decode(null, bbDecode);
+                               processEntry(commitPart);
+                       }
+               }
 
-               return new Pair<Boolean, List<Commit>>(true, returnCommits);
-       }
+               updateLiveStateFromLocal();
 
-       public void decrementLiveCount() {
-               liveslotcount--;
+               return true;
        }
 
-       private void setResizeThreshold() {
-               int resize_lower = (int) (RESIZE_THRESHOLD * numslots);
-               resizethreshold = resize_lower - 1 + random.nextInt(numslots - resize_lower);
-       }
+       private Pair<Boolean, Boolean> sendTransactionToLocal(Transaction transaction) {
 
-       private boolean tryput(PendingTransaction pendingTrans, boolean resize) throws ServerException {
-               Slot s = new Slot(this, sequencenumber + 1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
+               // Get the devices local communications
+               Pair<String, Integer> localCommunicationInformation = localCommunicationTable.get(transaction.getArbitrator());
 
-               int newsize = 0;
-               if (liveslotcount > resizethreshold) {
-                       resize = true; //Resize is forced
+               if (localCommunicationInformation == null) {
+                       // Cant talk to that device locally so do nothing
+                       return new Pair<Boolean, Boolean>(true, false);
                }
 
-               if (resize) {
-                       newsize = (int) (numslots * RESIZE_MULTIPLE);
-                       TableStatus status = new TableStatus(s, newsize);
-                       s.addEntry(status);
+               // Get the size of the send data
+               int sendDataSize = Integer.BYTES + Long.BYTES;
+               for (TransactionPart part : transaction.getParts().values()) {
+                       sendDataSize += part.getSize();
                }
 
-               doRejectedMessages(s);
+               Long lastArbitrationDataLocalSequenceNumber = (long) - 1;
+               if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(transaction.getArbitrator()) != null) {
+                       lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(transaction.getArbitrator());
+               }
 
-               ThreeTuple<Boolean, Boolean, Long> retTup = doMandatoryResuce(s, resize);
+               // Make the send data size
+               byte[] sendData = new byte[sendDataSize];
+               ByteBuffer bbEncode = ByteBuffer.wrap(sendData);
 
-               // Resize was needed so redo call
-               if (retTup.getFirst()) {
-                       return tryput(pendingTrans, true);
+               // Encode the data
+               bbEncode.putLong(lastArbitrationDataLocalSequenceNumber);
+               bbEncode.putInt(transaction.getParts().size());
+               for (TransactionPart part : transaction.getParts().values()) {
+                       part.encode(bbEncode);
                }
 
-               // Extract working variables
-               boolean seenliveslot = retTup.getSecond();
-               long seqn = retTup.getThird();
 
-               doArbitration(s);
+               // Send by local
+               byte[] returnData = cloud.sendLocalData(sendData, localSequenceNumber, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
+               localSequenceNumber++;
 
-               Transaction trans = new Transaction(s,
-                                                   s.getSequenceNumber(),
-                                                   localmachineid,
-                                                   pendingTrans.getArbitrator(),
-                                                   pendingTrans.getKVUpdates(),
-                                                   pendingTrans.getKVGuard());
-               boolean insertedTrans = false;
-               if (s.hasSpace(trans)) {
-                       s.addEntry(trans);
-                       insertedTrans = true;
+               if (returnData == null) {
+                       // Could not contact server
+                       return new Pair<Boolean, Boolean>(true, false);
                }
 
-               doOptionalRescue(s, seenliveslot, seqn, resize);
-               Pair<Boolean, Slot[]> sendRetData = doSendSlots(s, insertedTrans, resize, newsize);
+               // Decode the data
+               ByteBuffer bbDecode = ByteBuffer.wrap(returnData);
+               boolean didCommit = bbDecode.get() == 1;
+               boolean couldArbitrate = bbDecode.get() == 1;
+               int numberOfEntries = bbDecode.getInt();
+               boolean foundAbort = false;
+
+               for (int i = 0; i < numberOfEntries; i++) {
+                       byte type = bbDecode.get();
+                       if (type == Entry.TypeAbort) {
+                               Abort abort = (Abort)Abort.decode(null, bbDecode);
 
-               if (sendRetData.getFirst()) {
-                       // update the status and change what the sequence number is for the
-                       TransactionStatus transStatus = transactionStatusMap.remove(pendingTrans.getMachineLocalTransSeqNum());
-                       transStatus.setStatus(TransactionStatus.StatusSent);
-                       transStatus.setSentTransaction();
-                       transactionStatusMap.put(trans.getSequenceNumber(), transStatus);
+                               if ((abort.getTransactionMachineId() == localMachineId) && (abort.getTransactionClientLocalSequenceNumber() == transaction.getClientLocalSequenceNumber())) {
+                                       foundAbort = true;
+                               }
+
+                               processEntry(abort);
+                       } else if (type == Entry.TypeCommitPart) {
+                               CommitPart commitPart = (CommitPart)CommitPart.decode(null, bbDecode);
+                               processEntry(commitPart);
+                       }
                }
 
+               updateLiveStateFromLocal();
 
-               if (sendRetData.getSecond().length != 0) {
-                       // insert into the local block chain
-                       validateandupdate(sendRetData.getSecond(), true);
+               if (couldArbitrate) {
+                       TransactionStatus status =  transaction.getTransactionStatus();
+                       if (didCommit) {
+                               status.setStatus(TransactionStatus.StatusCommitted);
+                       } else {
+                               status.setStatus(TransactionStatus.StatusAborted);
+                       }
+               } else {
+                       TransactionStatus status =  transaction.getTransactionStatus();
+                       if (foundAbort) {
+                               status.setStatus(TransactionStatus.StatusAborted);
+                       } else {
+                               status.setStatus(TransactionStatus.StatusCommitted);
+                       }
                }
 
-               return sendRetData.getFirst();
+               return new Pair<Boolean, Boolean>(false, true);
        }
 
-       private boolean tryput(IoTString keyName, long arbMachineid, boolean resize) throws ServerException {
-               Slot s = new Slot(this, sequencenumber + 1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
-               int newsize = 0;
-               if (liveslotcount > resizethreshold) {
-                       resize = true; //Resize is forced
-               }
+       public synchronized byte[] acceptDataFromLocal(byte[] data) {
 
-               if (resize) {
-                       newsize = (int) (numslots * RESIZE_MULTIPLE);
-                       TableStatus status = new TableStatus(s, newsize);
-                       s.addEntry(status);
+               // Decode the data
+               ByteBuffer bbDecode = ByteBuffer.wrap(data);
+               long lastArbitratedSequenceNumberSeen = bbDecode.getLong();
+               int numberOfParts = bbDecode.getInt();
+
+               // If we did commit a transaction or not
+               boolean didCommit = false;
+               boolean couldArbitrate = false;
+
+               if (numberOfParts != 0) {
+
+                       // decode the transaction
+                       Transaction transaction = new Transaction();
+                       for (int i = 0; i < numberOfParts; i++) {
+                               bbDecode.get();
+                               TransactionPart newPart = (TransactionPart)TransactionPart.decode(null, bbDecode);
+                               transaction.addPartDecode(newPart);
+                       }
+
+                       // Arbitrate on transaction and pull relevant return data
+                       Pair<Boolean, Boolean> localArbitrateReturn = arbitrateOnLocalTransaction(transaction);
+                       couldArbitrate = localArbitrateReturn.getFirst();
+                       didCommit = localArbitrateReturn.getSecond();
+
+                       updateLiveStateFromLocal();
+
+                       // Transaction was sent to the server so keep track of it to prevent double commit
+                       if (transaction.getSequenceNumber() != -1) {
+                               offlineTransactionsCommittedAndAtServer.add(transaction.getId());
+                       }
                }
 
-               doRejectedMessages(s);
-               ThreeTuple<Boolean, Boolean, Long> retTup = doMandatoryResuce(s, resize);
+               // The data to send back
+               int returnDataSize = 0;
+               List<Entry> unseenArbitrations = new ArrayList<Entry>();
+
+               // Get the aborts to send back
+               List<Long> abortLocalSequenceNumbers = new ArrayList<Long >(liveAbortsGeneratedByLocal.keySet());
+               Collections.sort(abortLocalSequenceNumbers);
+               for (Long localSequenceNumber : abortLocalSequenceNumbers) {
+                       if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
+                               continue;
+                       }
 
-               // Resize was needed so redo call
-               if (retTup.getFirst()) {
-                       return tryput(keyName, arbMachineid, true);
+                       Abort abort = liveAbortsGeneratedByLocal.get(localSequenceNumber);
+                       unseenArbitrations.add(abort);
+                       returnDataSize += abort.getSize();
                }
 
-               // Extract working variables
-               boolean seenliveslot = retTup.getSecond();
-               long seqn = retTup.getThird();
+               // Get the commits to send back
+               Map<Long, Commit> commitForClientTable = liveCommitsTable.get(localMachineId);
+               if (commitForClientTable != null) {
+                       List<Long> commitLocalSequenceNumbers = new ArrayList<Long>(commitForClientTable.keySet());
+                       Collections.sort(commitLocalSequenceNumbers);
+
+                       for (Long localSequenceNumber : commitLocalSequenceNumbers) {
+                               Commit commit = commitForClientTable.get(localSequenceNumber);
 
-               doArbitration(s);
+                               if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
+                                       continue;
+                               }
 
-               NewKey newKey = new NewKey(s, keyName, arbMachineid);
+                               unseenArbitrations.addAll(commit.getParts().values());
 
-               boolean insertedNewKey = false;
-               if (s.hasSpace(newKey)) {
-                       s.addEntry(newKey);
-                       insertedNewKey = true;
+                               for (CommitPart commitPart : commit.getParts().values()) {
+                                       returnDataSize += commitPart.getSize();
+                               }
+                       }
                }
 
-               doOptionalRescue(s, seenliveslot, seqn, resize);
-               Pair<Boolean, Slot[]> sendRetData = doSendSlots(s, insertedNewKey, resize, newsize);
+               // Number of arbitration entries to decode
+               returnDataSize += 2 * Integer.BYTES;
 
-               if (sendRetData.getSecond().length != 0) {
-                       // insert into the local block chain
-                       validateandupdate(sendRetData.getSecond(), true);
+               // Boolean of did commit or not
+               if (numberOfParts != 0) {
+                       returnDataSize += Byte.BYTES;
                }
 
-               return sendRetData.getFirst();
-       }
-
-       private void doRejectedMessages(Slot s) {
-               if (! rejectedmessagelist.isEmpty()) {
-                       /* TODO: We should avoid generating a rejected message entry if
-                        * there is already a sufficient entry in the queue (e.g.,
-                        * equalsto value of true and same sequence number).  */
+               // Data to send Back
+               byte[] returnData = new byte[returnDataSize];
+               ByteBuffer bbEncode = ByteBuffer.wrap(returnData);
 
-                       long old_seqn = rejectedmessagelist.firstElement();
-                       if (rejectedmessagelist.size() > REJECTED_THRESHOLD) {
-                               long new_seqn = rejectedmessagelist.lastElement();
-                               RejectedMessage rm = new RejectedMessage(s, localmachineid, old_seqn, new_seqn, false);
-                               s.addEntry(rm);
+               if (numberOfParts != 0) {
+                       if (didCommit) {
+                               bbEncode.put((byte)1);
                        } else {
-                               long prev_seqn = -1;
+                               bbEncode.put((byte)0);
+                       }
+                       if (couldArbitrate) {
+                               bbEncode.put((byte)1);
+                       } else {
+                               bbEncode.put((byte)0);
+                       }
+               }
+
+               bbEncode.putInt(unseenArbitrations.size());
+               for (Entry entry : unseenArbitrations) {
+                       entry.encode(bbEncode);
+               }
+
+
+               localSequenceNumber++;
+               return returnData;
+       }
+
+       private ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsToServer(Slot slot, int newSize, boolean isNewKey)  throws ServerException {
+
+               boolean attemptedToSendToServerTmp = attemptedToSendToServer;
+               attemptedToSendToServer = true;
+
+               boolean inserted = false;
+               boolean lastTryInserted = false;
+
+               Slot[] array = cloud.putSlot(slot, newSize);
+               if (array == null) {
+                       array = new Slot[] {slot};
+                       rejectedSlotList.clear();
+                       inserted = true;
+               }       else {
+                       if (array.length == 0) {
+                               throw new Error("Server Error: Did not send any slots");
+                       }
+
+                       // if (attemptedToSendToServerTmp) {
+                       if (hadPartialSendToServer) {
+
+                               boolean isInserted = false;
+                               for (Slot s : array) {
+                                       if ((s.getSequenceNumber() == slot.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
+                                               isInserted = true;
+                                               break;
+                                       }
+                               }
+
+                               for (Slot s : array) {
+                                       if (isInserted) {
+                                               break;
+                                       }
+
+                                       // Process each entry in the slot
+                                       for (Entry entry : s.getEntries()) {
+
+                                               if (entry.getType() == Entry.TypeLastMessage) {
+                                                       LastMessage lastMessage = (LastMessage)entry;
+
+                                                       if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == slot.getSequenceNumber())) {
+                                                               isInserted = true;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                               }
+
+                               if (!isInserted) {
+                                       rejectedSlotList.add(slot.getSequenceNumber());
+                                       lastTryInserted = false;
+                               } else {
+                                       lastTryInserted = true;
+                               }
+                       } else {
+                               rejectedSlotList.add(slot.getSequenceNumber());
+                               lastTryInserted = false;
+                       }
+               }
+
+               return new ThreeTuple<Boolean, Boolean, Slot[]>(inserted, lastTryInserted, array);
+       }
+
+       /**
+        * Returns false if a resize was needed
+        */
+       private ThreeTuple<Boolean, Integer, Boolean> fillSlot(Slot slot, boolean resize, NewKey newKeyEntry) {
+
+
+               int newSize = 0;
+               if (liveSlotCount > bufferResizeThreshold) {
+                       resize = true; //Resize is forced
+
+               }
+
+               if (resize) {
+                       newSize = (int) (numberOfSlots * RESIZE_MULTIPLE);
+                       TableStatus status = new TableStatus(slot, newSize);
+                       slot.addEntry(status);
+               }
+
+               // Fill with rejected slots first before doing anything else
+               doRejectedMessages(slot);
+
+               // Do mandatory rescue of entries
+               ThreeTuple<Boolean, Boolean, Long> mandatoryRescueReturn = doMandatoryResuce(slot, resize);
+
+               // Extract working variables
+               boolean needsResize = mandatoryRescueReturn.getFirst();
+               boolean seenLiveSlot = mandatoryRescueReturn.getSecond();
+               long currentRescueSequenceNumber = mandatoryRescueReturn.getThird();
+
+               if (needsResize && !resize) {
+                       // We need to resize but we are not resizing so return false
+                       return new ThreeTuple<Boolean, Integer, Boolean>(true, null, null);
+               }
+
+               boolean inserted = false;
+               if (newKeyEntry != null) {
+                       newKeyEntry.setSlot(slot);
+                       if (slot.hasSpace(newKeyEntry)) {
+
+                               slot.addEntry(newKeyEntry);
+                               inserted = true;
+                       }
+               }
+
+               // Clear the transactions, aborts and commits that were sent previously
+               transactionPartsSent.clear();
+               pendingSendArbitrationEntriesToDelete.clear();
+
+               for (ArbitrationRound round : pendingSendArbitrationRounds) {
+                       boolean isFull = false;
+                       round.generateParts();
+                       List<Entry> parts = round.getParts();
+
+                       // Insert pending arbitration data
+                       for (Entry arbitrationData : parts) {
+
+                               // If it is an abort then we need to set some information
+                               if (arbitrationData instanceof Abort) {
+                                       ((Abort)arbitrationData).setSequenceNumber(slot.getSequenceNumber());
+                               }
+
+                               if (!slot.hasSpace(arbitrationData)) {
+                                       // No space so cant do anything else with these data entries
+                                       isFull = true;
+                                       break;
+                               }
+
+                               // Add to this current slot and add it to entries to delete
+                               slot.addEntry(arbitrationData);
+                               pendingSendArbitrationEntriesToDelete.add(arbitrationData);
+                       }
+
+                       if (isFull) {
+                               break;
+                       }
+               }
+
+               if (pendingTransactionQueue.size() > 0) {
+
+                       Transaction transaction = pendingTransactionQueue.get(0);
+
+                       // Set the transaction sequence number if it has yet to be inserted into the block chain
+                       // if ((!transaction.didSendAPartToServer() && !transaction.getServerFailure()) || (transaction.getSequenceNumber() == -1)) {
+                       //      transaction.setSequenceNumber(slot.getSequenceNumber());
+                       // }
+
+                       if ((!transaction.didSendAPartToServer()) || (transaction.getSequenceNumber() == -1)) {
+                               transaction.setSequenceNumber(slot.getSequenceNumber());
+                       }
+
+
+                       while (true) {
+                               TransactionPart part = transaction.getNextPartToSend();
+
+                               if (part == null) {
+                                       // Ran out of parts to send for this transaction so move on
+                                       break;
+                               }
+
+                               if (slot.hasSpace(part)) {
+                                       slot.addEntry(part);
+                                       List<Integer> partsSent = transactionPartsSent.get(transaction);
+                                       if (partsSent == null) {
+                                               partsSent = new ArrayList<Integer>();
+                                               transactionPartsSent.put(transaction, partsSent);
+                                       }
+                                       partsSent.add(part.getPartNumber());
+                                       transactionPartsSent.put(transaction, partsSent);
+                               } else {
+                                       break;
+                               }
+                       }
+               }
+
+               // Fill the remainder of the slot with rescue data
+               doOptionalRescue(slot, seenLiveSlot, currentRescueSequenceNumber, resize);
+
+               return new ThreeTuple<Boolean, Integer, Boolean>(false, newSize, inserted);
+       }
+
+       private void doRejectedMessages(Slot s) {
+               if (! rejectedSlotList.isEmpty()) {
+                       /* TODO: We should avoid generating a rejected message entry if
+                        * there is already a sufficient entry in the queue (e.g.,
+                        * equalsto value of true and same sequence number).  */
+
+                       long old_seqn = rejectedSlotList.firstElement();
+                       if (rejectedSlotList.size() > REJECTED_THRESHOLD) {
+                               long new_seqn = rejectedSlotList.lastElement();
+                               RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), localMachineId, old_seqn, new_seqn, false);
+                               s.addEntry(rm);
+                       } else {
+                               long prev_seqn = -1;
                                int i = 0;
                                /* Go through list of missing messages */
-                               for (; i < rejectedmessagelist.size(); i++) {
-                                       long curr_seqn = rejectedmessagelist.get(i);
+                               for (; i < rejectedSlotList.size(); i++) {
+                                       long curr_seqn = rejectedSlotList.get(i);
                                        Slot s_msg = buffer.getSlot(curr_seqn);
                                        if (s_msg != null)
                                                break;
@@ -856,657 +1369,1309 @@ final public class Table {
                                }
                                /* Generate rejected message entry for missing messages */
                                if (prev_seqn != -1) {
-                                       RejectedMessage rm = new RejectedMessage(s, localmachineid, old_seqn, prev_seqn, false);
+                                       RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), localMachineId, old_seqn, prev_seqn, false);
                                        s.addEntry(rm);
                                }
                                /* Generate rejected message entries for present messages */
-                               for (; i < rejectedmessagelist.size(); i++) {
-                                       long curr_seqn = rejectedmessagelist.get(i);
+                               for (; i < rejectedSlotList.size(); i++) {
+                                       long curr_seqn = rejectedSlotList.get(i);
                                        Slot s_msg = buffer.getSlot(curr_seqn);
                                        long machineid = s_msg.getMachineID();
-                                       RejectedMessage rm = new RejectedMessage(s, machineid, curr_seqn, curr_seqn, true);
+                                       RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), machineid, curr_seqn, curr_seqn, true);
                                        s.addEntry(rm);
                                }
                        }
                }
        }
 
-       private ThreeTuple<Boolean, Boolean, Long> doMandatoryResuce(Slot s, boolean resize) {
-               long newestseqnum = buffer.getNewestSeqNum();
-               long oldestseqnum = buffer.getOldestSeqNum();
-               if (lastliveslotseqn < oldestseqnum)
-                       lastliveslotseqn = oldestseqnum;
+       private ThreeTuple<Boolean, Boolean, Long> doMandatoryResuce(Slot slot, boolean resize) {
+               long newestSequenceNumber = buffer.getNewestSeqNum();
+               long oldestSequenceNumber = buffer.getOldestSeqNum();
+               if (oldestLiveSlotSequenceNumver < oldestSequenceNumber) {
+                       oldestLiveSlotSequenceNumver = oldestSequenceNumber;
+               }
 
-               long seqn = lastliveslotseqn;
-               boolean seenliveslot = false;
-               long firstiffull = newestseqnum + 1 - numslots; // smallest seq number in the buffer if it is full
-               long threshold = firstiffull + FREE_SLOTS;      // we want the buffer to be clear of live entries up to this point
+               long currentSequenceNumber = oldestLiveSlotSequenceNumver;
+               boolean seenLiveSlot = false;
+               long firstIfFull = newestSequenceNumber + 1 - numberOfSlots;    // smallest seq number in the buffer if it is full
+               long threshold = firstIfFull + FREE_SLOTS;      // we want the buffer to be clear of live entries up to this point
 
 
                // Mandatory Rescue
-               for (; seqn < threshold; seqn++) {
-                       Slot prevslot = buffer.getSlot(seqn);
+               for (; currentSequenceNumber < threshold; currentSequenceNumber++) {
+                       Slot previousSlot = buffer.getSlot(currentSequenceNumber);
                        // Push slot number forward
-                       if (! seenliveslot)
-                               lastliveslotseqn = seqn;
+                       if (! seenLiveSlot) {
+                               oldestLiveSlotSequenceNumver = currentSequenceNumber;
+                       }
 
-                       if (! prevslot.isLive())
+                       if (!previousSlot.isLive()) {
+                               continue;
+                       }
+
+                       // We have seen a live slot
+                       seenLiveSlot = true;
+
+                       // Get all the live entries for a slot
+                       Vector<Entry> liveEntries = previousSlot.getLiveEntries(resize);
+
+                       // Iterate over all the live entries and try to rescue them
+                       for (Entry liveEntry : liveEntries) {
+                               if (slot.hasSpace(liveEntry)) {
+
+                                       // Enough space to rescue the entry
+                                       slot.addEntry(liveEntry);
+                               } else if (currentSequenceNumber == firstIfFull) {
+                                       //if there's no space but the entry is about to fall off the queue
+                                       System.out.println("B"); //?
+                                       return new ThreeTuple<Boolean, Boolean, Long>(true, seenLiveSlot, currentSequenceNumber);
+
+                               }
+                       }
+               }
+
+               // Did not resize
+               return new ThreeTuple<Boolean, Boolean, Long>(false, seenLiveSlot, currentSequenceNumber);
+       }
+
+       private void  doOptionalRescue(Slot s, boolean seenliveslot, long seqn, boolean resize) {
+               /* now go through live entries from least to greatest sequence number until
+                * either all live slots added, or the slot doesn't have enough room
+                * for SKIP_THRESHOLD consecutive entries*/
+               int skipcount = 0;
+               long newestseqnum = buffer.getNewestSeqNum();
+               search:
+               for (; seqn <= newestseqnum; seqn++) {
+                       Slot prevslot = buffer.getSlot(seqn);
+                       //Push slot number forward
+                       if (!seenliveslot)
+                               oldestLiveSlotSequenceNumver = seqn;
+
+                       if (!prevslot.isLive())
                                continue;
                        seenliveslot = true;
                        Vector<Entry> liveentries = prevslot.getLiveEntries(resize);
                        for (Entry liveentry : liveentries) {
-                               if (s.hasSpace(liveentry)) {
+                               if (s.hasSpace(liveentry))
                                        s.addEntry(liveentry);
-                               } else if (seqn == firstiffull) { //if there's no space but the entry is about to fall off the queue
-                                       if (!resize) {
-                                               System.out.println("B"); //?
-                                               return new ThreeTuple<Boolean, Boolean, Long>(true, seenliveslot, seqn);
-                                       }
+                               else {
+                                       skipcount++;
+                                       if (skipcount > SKIP_THRESHOLD)
+                                               break search;
                                }
                        }
                }
+       }
 
-               // Did not resize
-               return new ThreeTuple<Boolean, Boolean, Long>(false, seenliveslot, seqn);
+       /**
+        * Checks for malicious activity and updates the local copy of the block chain.
+        */
+       private void validateAndUpdate(Slot[] newSlots, boolean acceptUpdatesToLocal) {
+
+               // The cloud communication layer has checked slot HMACs already before decoding
+               if (newSlots.length == 0) {
+                       return;
+               }
+
+               // Make sure all slots are newer than the last largest slot this client has seen
+               long firstSeqNum = newSlots[0].getSequenceNumber();
+               if (firstSeqNum <= sequenceNumber) {
+                       throw new Error("Server Error: Sent older slots!");
+               }
+
+               // Create an object that can access both new slots and slots in our local chain
+               // without committing slots to our local chain
+               SlotIndexer indexer = new SlotIndexer(newSlots, buffer);
+
+               // Check that the HMAC chain is not broken
+               checkHMACChain(indexer, newSlots);
+
+               // Set to keep track of messages from clients
+               HashSet<Long> machineSet = new HashSet<Long>(lastMessageTable.keySet());
+
+               // Process each slots data
+               for (Slot slot : newSlots) {
+                       processSlot(indexer, slot, acceptUpdatesToLocal, machineSet);
+
+                       updateExpectedSize();
+               }
+
+               // If there is a gap, check to see if the server sent us everything.
+               if (firstSeqNum != (sequenceNumber + 1)) {
+
+                       // Check the size of the slots that were sent down by the server.
+                       // Can only check the size if there was a gap
+                       checkNumSlots(newSlots.length);
+
+                       // Since there was a gap every machine must have pushed a slot or must have
+                       // a last message message.  If not then the server is hiding slots
+                       if (!machineSet.isEmpty()) {
+                               throw new Error("Missing record for machines: " + machineSet);
+                       }
+               }
+
+               // Update the size of our local block chain.
+               commitNewMaxSize();
+
+               // Commit new to slots to the local block chain.
+               for (Slot slot : newSlots) {
+
+                       // Insert this slot into our local block chain copy.
+                       buffer.putSlot(slot);
+
+                       // Keep track of how many slots are currently live (have live data in them).
+                       liveSlotCount++;
+               }
+
+               // Get the sequence number of the latest slot in the system
+               sequenceNumber = newSlots[newSlots.length - 1].getSequenceNumber();
+
+               updateLiveStateFromServer();
+
+               // No Need to remember after we pulled from the server
+               offlineTransactionsCommittedAndAtServer.clear();
+
+               // This is invalidated now
+               hadPartialSendToServer = false;
        }
 
-       private boolean doArbitration(Slot s) {
+       private void updateLiveStateFromServer() {
+               // Process the new transaction parts
+               processNewTransactionParts();
 
-               // flag whether we have finished all arbitration
-               boolean stillHasArbitration = false;
+               // Do arbitration on new transactions that were received
+               arbitrateFromServer();
 
-               pendingCommitsToDelete.clear();
+               // Update all the committed keys
+               boolean didCommitOrSpeculate = updateCommittedTable();
 
-               // First add queue commits
-               for (Commit commit : pendingCommitsList) {
-                       if (s.hasSpace(commit)) {
-                               s.addEntry(commit);
-                               pendingCommitsToDelete.add(commit);
-                       } else {
-                               // Ran out of space so move on but still not done
-                               stillHasArbitration = true;
-                               return stillHasArbitration;
+               // Delete the transactions that are now dead
+               updateLiveTransactionsAndStatus();
+
+               // Do speculations
+               didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate);
+               updatePendingTransactionSpeculativeTable(didCommitOrSpeculate);
+       }
+
+       private void updateLiveStateFromLocal() {
+               // Update all the committed keys
+               boolean didCommitOrSpeculate = updateCommittedTable();
+
+               // Delete the transactions that are now dead
+               updateLiveTransactionsAndStatus();
+
+               // Do speculations
+               didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate);
+               updatePendingTransactionSpeculativeTable(didCommitOrSpeculate);
+       }
+
+       private void initExpectedSize(long firstSequenceNumber, long numberOfSlots) {
+               // if (didFindTableStatus) {
+               // return;
+               // }
+               long prevslots = firstSequenceNumber;
+
+
+               if (didFindTableStatus) {
+                       // expectedsize = (prevslots < ((long) numberOfSlots)) ? (int) prevslots : expectedsize;
+                       // System.out.println("Here2: " + expectedsize + "    " + numberOfSlots + "   " + prevslots);
+
+               } else {
+                       expectedsize = (prevslots < ((long) numberOfSlots)) ? (int) prevslots : numberOfSlots;
+                       // System.out.println("Here: " + expectedsize);
+               }
+
+               // System.out.println(numberOfSlots);
+
+               didFindTableStatus = true;
+               currMaxSize = numberOfSlots;
+       }
+
+       private void updateExpectedSize() {
+               expectedsize++;
+
+               if (expectedsize > currMaxSize) {
+                       expectedsize = currMaxSize;
+               }
+       }
+
+
+       /**
+        * Check the size of the block chain to make sure there are enough slots sent back by the server.
+        * This is only called when we have a gap between the slots that we have locally and the slots
+        * sent by the server therefore in the slots sent by the server there will be at least 1 Table
+        * status message
+        */
+       private void checkNumSlots(int numberOfSlots) {
+               if (numberOfSlots != expectedsize) {
+                       throw new Error("Server Error: Server did not send all slots.  Expected: " + expectedsize + " Received:" + numberOfSlots);
+               }
+       }
+
+       private void updateCurrMaxSize(int newmaxsize) {
+               currMaxSize = newmaxsize;
+       }
+
+
+       /**
+        * Update the size of of the local buffer if it is needed.
+        */
+       private void commitNewMaxSize() {
+               didFindTableStatus = false;
+
+               // Resize the local slot buffer
+               if (numberOfSlots != currMaxSize) {
+                       buffer.resize((int)currMaxSize);
+               }
+
+               // Change the number of local slots to the new size
+               numberOfSlots = (int)currMaxSize;
+
+
+               // Recalculate the resize threshold since the size of the local buffer has changed
+               setResizeThreshold();
+       }
+
+       /**
+        * Process the new transaction parts from this latest round of slots received from the server
+        */
+       private void processNewTransactionParts() {
+
+               if (newTransactionParts.size() == 0) {
+                       // Nothing new to process
+                       return;
+               }
+
+               // Iterate through all the machine Ids that we received new parts for
+               for (Long machineId : newTransactionParts.keySet()) {
+                       Map<Pair<Long, Integer>, TransactionPart> parts = newTransactionParts.get(machineId);
+
+                       // Iterate through all the parts for that machine Id
+                       for (Pair<Long, Integer> partId : parts.keySet()) {
+                               TransactionPart part = parts.get(partId);
+
+                               Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(part.getArbitratorId());
+                               if ((lastTransactionNumber != null) && (lastTransactionNumber >= part.getSequenceNumber())) {
+                                       // Set dead the transaction part
+                                       part.setDead();
+                                       continue;
+                               }
+
+                               // Get the transaction object for that sequence number
+                               Transaction transaction = liveTransactionBySequenceNumberTable.get(part.getSequenceNumber());
+
+                               if (transaction == null) {
+                                       // This is a new transaction that we dont have so make a new one
+                                       transaction = new Transaction();
+
+                                       // Insert this new transaction into the live tables
+                                       liveTransactionBySequenceNumberTable.put(part.getSequenceNumber(), transaction);
+                                       liveTransactionByTransactionIdTable.put(part.getTransactionId(), transaction);
+                               }
+
+                               // Add that part to the transaction
+                               transaction.addPartDecode(part);
                        }
                }
 
-               // Arbitrate
+               // Clear all the new transaction parts in preparation for the next time the server sends slots
+               newTransactionParts.clear();
+       }
+
+
+       private long lastSeqNumArbOn = 0;
+
+       private void arbitrateFromServer() {
+
+               if (liveTransactionBySequenceNumberTable.size() == 0) {
+                       // Nothing to arbitrate on so move on
+                       return;
+               }
+
+               // Get the transaction sequence numbers and sort from oldest to newest
+               List<Long> transactionSequenceNumbers = new ArrayList<Long>(liveTransactionBySequenceNumberTable.keySet());
+               Collections.sort(transactionSequenceNumbers);
+
+               // Collection of key value pairs that are
                Map<IoTString, KeyValue> speculativeTableTmp = new HashMap<IoTString, KeyValue>();
-               List<Long> transSeqNums = new ArrayList<Long>(uncommittedTransactionsMap.keySet());
 
-               // Sort from oldest to newest
-               Collections.sort(transSeqNums);
+               // The last transaction arbitrated on
+               long lastTransactionCommitted = -1;
+               Set<Abort> generatedAborts = new HashSet<Abort>();
 
-               for (Long transNum : transSeqNums) {
-                       Transaction ut = uncommittedTransactionsMap.get(transNum);
+               for (Long transactionSequenceNumber : transactionSequenceNumbers) {
+                       Transaction transaction = liveTransactionBySequenceNumberTable.get(transactionSequenceNumber);
 
-                       // Check if this machine arbitrates for this transaction
-                       if (ut.getArbitrator() != localmachineid ) {
+
+
+                       // Check if this machine arbitrates for this transaction if not then we cant arbitrate this transaction
+                       if (transaction.getArbitrator() != localMachineId) {
                                continue;
                        }
 
-                       // we did have something to arbitrate on
-                       stillHasArbitration = true;
+                       if (transactionSequenceNumber < lastSeqNumArbOn) {
+                               continue;
+                       }
 
-                       Entry newEntry = null;
+                       if (offlineTransactionsCommittedAndAtServer.contains(transaction.getId())) {
+                               // We have seen this already locally so dont commit again
+                               continue;
+                       }
 
-                       if (ut.evaluateGuard(commitedTable, speculativeTableTmp)) {
+
+                       if (!transaction.isComplete()) {
+                               // Will arbitrate in incorrect order if we continue so just break
+                               // Most likely this
+                               break;
+                       }
+
+
+                       // update the largest transaction seen by arbitrator from server
+                       if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) == null) {
+                               lastTransactionSeenFromMachineFromServer.put(transaction.getMachineId(), transaction.getClientLocalSequenceNumber());
+                       } else {
+                               Long lastTransactionSeenFromMachine = lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId());
+                               if (transaction.getClientLocalSequenceNumber() > lastTransactionSeenFromMachine) {
+                                       lastTransactionSeenFromMachineFromServer.put(transaction.getMachineId(), transaction.getClientLocalSequenceNumber());
+                               }
+                       }
+
+                       if (transaction.evaluateGuard(committedKeyValueTable, speculativeTableTmp, null)) {
                                // Guard evaluated as true
 
-                               // update the local tmp current key set
-                               for (KeyValue kv : ut.getkeyValueUpdateSet()) {
+                               // Update the local changes so we can make the commit
+                               for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
                                        speculativeTableTmp.put(kv.getKey(), kv);
                                }
 
-                               // create the commit
-                               newEntry = new Commit(s,
-                                                     ut.getSequenceNumber(),
-                                                     commitSequenceNumber,
-                                                     ut.getArbitrator(),
-                                                     ut.getkeyValueUpdateSet());
-                               commitSequenceNumber = commitSequenceNumber + 1;
+                               // Update what the last transaction committed was for use in batch commit
+                               lastTransactionCommitted = transactionSequenceNumber;
                        } else {
-                               // Guard was false
+                               // Guard evaluated was false so create abort
+
+                               // Create the abort
+                               Abort newAbort = new Abort(null,
+                                                          transaction.getClientLocalSequenceNumber(),
+                                                          transaction.getSequenceNumber(),
+                                                          transaction.getMachineId(),
+                                                          transaction.getArbitrator(),
+                                                          localArbitrationSequenceNumber);
+                               localArbitrationSequenceNumber++;
+
+                               generatedAborts.add(newAbort);
 
-                               // create the abort
-                               newEntry = new Abort(s,
-                                                    ut.getSequenceNumber(),
-                                                    ut.getMachineID(),
-                                                    ut.getArbitrator());
+                               // Insert the abort so we can process
+                               processEntry(newAbort);
                        }
 
-                       if ((newEntry != null) && s.hasSpace(newEntry)) {
-                               s.addEntry(newEntry);
-                       } else {
-                               break;
+                       lastSeqNumArbOn = transactionSequenceNumber;
+
+                       // liveTransactionBySequenceNumberTable.remove(transactionSequenceNumber);
+               }
+
+               Commit newCommit = null;
+
+               // If there is something to commit
+               if (speculativeTableTmp.size() != 0) {
+
+                       // Create the commit and increment the commit sequence number
+                       newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, lastTransactionCommitted);
+                       localArbitrationSequenceNumber++;
+
+                       // Add all the new keys to the commit
+                       for (KeyValue kv : speculativeTableTmp.values()) {
+                               newCommit.addKV(kv);
+                       }
+
+                       // create the commit parts
+                       newCommit.createCommitParts();
+
+                       // Append all the commit parts to the end of the pending queue waiting for sending to the server
+
+                       // Insert the commit so we can process it
+                       for (CommitPart commitPart : newCommit.getParts().values()) {
+                               processEntry(commitPart);
                        }
                }
 
-               return stillHasArbitration;
-       }
+               if ((newCommit != null) || (generatedAborts.size() > 0)) {
+                       ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, generatedAborts);
+                       pendingSendArbitrationRounds.add(arbitrationRound);
 
-       private void deletePendingCommits() {
-               for (Commit com : pendingCommitsToDelete) {
-                       pendingCommitsList.remove(com);
+                       if (compactArbitrationData()) {
+                               ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
+                               if (newArbitrationRound.getCommit() != null) {
+                                       for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
+                                               processEntry(commitPart);
+                                       }
+                               }
+                       }
                }
-               pendingCommitsToDelete.clear();
        }
 
-       private void  doOptionalRescue(Slot s, boolean seenliveslot, long seqn, boolean resize) {
-               /* now go through live entries from least to greatest sequence number until
-                * either all live slots added, or the slot doesn't have enough room
-                * for SKIP_THRESHOLD consecutive entries*/
-               int skipcount = 0;
-               long newestseqnum = buffer.getNewestSeqNum();
-               search:
-               for (; seqn <= newestseqnum; seqn++) {
-                       Slot prevslot = buffer.getSlot(seqn);
-                       //Push slot number forward
-                       if (!seenliveslot)
-                               lastliveslotseqn = seqn;
+       private Pair<Boolean, Boolean> arbitrateOnLocalTransaction(Transaction transaction) {
 
-                       if (!prevslot.isLive())
-                               continue;
-                       seenliveslot = true;
-                       Vector<Entry> liveentries = prevslot.getLiveEntries(resize);
-                       for (Entry liveentry : liveentries) {
-                               if (s.hasSpace(liveentry))
-                                       s.addEntry(liveentry);
-                               else {
-                                       skipcount++;
-                                       if (skipcount > SKIP_THRESHOLD)
-                                               break search;
+               // Check if this machine arbitrates for this transaction if not then we cant arbitrate this transaction
+               if (transaction.getArbitrator() != localMachineId) {
+                       return new Pair<Boolean, Boolean>(false, false);
+               }
+
+               if (!transaction.isComplete()) {
+                       // Will arbitrate in incorrect order if we continue so just break
+                       // Most likely this
+                       return new Pair<Boolean, Boolean>(false, false);
+               }
+
+               if (transaction.getMachineId() != localMachineId) {
+                       // dont do this check for local transactions
+                       if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) != null) {
+                               if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) > transaction.getClientLocalSequenceNumber()) {
+                                       // We've have already seen this from the server
+                                       return new Pair<Boolean, Boolean>(false, false);
                                }
                        }
                }
+
+               if (transaction.evaluateGuard(committedKeyValueTable, null, null)) {
+                       // Guard evaluated as true
+
+                       // Create the commit and increment the commit sequence number
+                       Commit newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, -1);
+                       localArbitrationSequenceNumber++;
+
+                       // Update the local changes so we can make the commit
+                       for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
+                               newCommit.addKV(kv);
+                       }
+
+                       // create the commit parts
+                       newCommit.createCommitParts();
+
+                       // Append all the commit parts to the end of the pending queue waiting for sending to the server
+                       ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, new HashSet<Abort>());
+                       pendingSendArbitrationRounds.add(arbitrationRound);
+
+                       if (compactArbitrationData()) {
+                               ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
+                               for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
+                                       processEntry(commitPart);
+                               }
+                       } else {
+                               // Insert the commit so we can process it
+                               for (CommitPart commitPart : newCommit.getParts().values()) {
+                                       processEntry(commitPart);
+                               }
+                       }
+
+                       if (transaction.getMachineId() == localMachineId) {
+                               TransactionStatus status = transaction.getTransactionStatus();
+                               if (status != null) {
+                                       status.setStatus(TransactionStatus.StatusCommitted);
+                               }
+                       }
+
+                       updateLiveStateFromLocal();
+                       return new Pair<Boolean, Boolean>(true, true);
+               } else {
+
+                       if (transaction.getMachineId() == localMachineId) {
+                               // For locally created messages update the status
+
+                               // Guard evaluated was false so create abort
+                               TransactionStatus status = transaction.getTransactionStatus();
+                               if (status != null) {
+                                       status.setStatus(TransactionStatus.StatusAborted);
+                               }
+                       } else {
+                               Set addAbortSet = new HashSet<Abort>();
+
+
+                               // Create the abort
+                               Abort newAbort = new Abort(null,
+                                                          transaction.getClientLocalSequenceNumber(),
+                                                          -1,
+                                                          transaction.getMachineId(),
+                                                          transaction.getArbitrator(),
+                                                          localArbitrationSequenceNumber);
+                               localArbitrationSequenceNumber++;
+
+                               addAbortSet.add(newAbort);
+
+
+                               // Append all the commit parts to the end of the pending queue waiting for sending to the server
+                               ArbitrationRound arbitrationRound = new ArbitrationRound(null, addAbortSet);
+                               pendingSendArbitrationRounds.add(arbitrationRound);
+
+                               if (compactArbitrationData()) {
+                                       ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
+                                       for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
+                                               processEntry(commitPart);
+                                       }
+                               }
+                       }
+
+                       updateLiveStateFromLocal();
+                       return new Pair<Boolean, Boolean>(true, false);
+               }
        }
 
-       private Pair<Boolean, Slot[]> doSendSlots(Slot s, boolean inserted, boolean resize, int newsize)  throws ServerException {
-               int max = 0;
-               if (resize)
-                       max = newsize;
+       /**
+        * Compacts the arbitration data my merging commits and aggregating aborts so that a single large push of commits can be done instead of many small updates
+        */
+       private boolean compactArbitrationData() {
 
-               Slot[] array = cloud.putSlot(s, max);
-               if (array == null) {
-                       array = new Slot[] {s};
-                       rejectedmessagelist.clear();
+               if (pendingSendArbitrationRounds.size() < 2) {
+                       // Nothing to compact so do nothing
+                       return false;
+               }
 
-                       // Delete pending commits that were sent to the cloud
-                       deletePendingCommits();
-               }       else {
-                       // if (array.length == 0)
-                       // throw new Error("Server Error: Did not send any slots");
-                       rejectedmessagelist.add(s.getSequenceNumber());
-                       inserted = false;
+               ArbitrationRound lastRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
+               if (lastRound.didSendPart()) {
+                       return false;
+               }
+
+               boolean hadCommit = (lastRound.getCommit() == null);
+               boolean gotNewCommit = false;
+
+               int numberToDelete = 1;
+               while (numberToDelete < pendingSendArbitrationRounds.size()) {
+                       ArbitrationRound round = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - numberToDelete - 1);
+
+                       if (round.isFull() || round.didSendPart()) {
+                               // Stop since there is a part that cannot be compacted and we need to compact in order
+                               break;
+                       }
+
+                       if (round.getCommit() == null) {
+
+                               // Try compacting aborts only
+                               int newSize = round.getCurrentSize() + lastRound.getAbortsCount();
+                               if (newSize > ArbitrationRound.MAX_PARTS) {
+                                       // Cant compact since it would be too large
+                                       break;
+                               }
+                               lastRound.addAborts(round.getAborts());
+                       } else {
+
+                               // Create a new larger commit
+                               Commit newCommit = Commit.merge(lastRound.getCommit(), round.getCommit(), localArbitrationSequenceNumber);
+                               localArbitrationSequenceNumber++;
+
+                               // Create the commit parts so that we can count them
+                               newCommit.createCommitParts();
+
+                               // Calculate the new size of the parts
+                               int newSize = newCommit.getNumberOfParts();
+                               newSize += lastRound.getAbortsCount();
+                               newSize += round.getAbortsCount();
+
+                               if (newSize > ArbitrationRound.MAX_PARTS) {
+                                       // Cant compact since it would be too large
+                                       break;
+                               }
+
+                               // Set the new compacted part
+                               lastRound.setCommit(newCommit);
+                               lastRound.addAborts(round.getAborts());
+                               gotNewCommit = true;
+                       }
+
+                       numberToDelete++;
                }
 
-               return new Pair<Boolean, Slot[]>(inserted, array);
-       }
+               if (numberToDelete != 1) {
+                       // If there is a compaction
 
-       private void validateandupdate(Slot[] newslots, boolean acceptupdatestolocal) {
-               /* The cloud communication layer has checked slot HMACs already
-                        before decoding */
-               if (newslots.length == 0) return;
+                       // Delete the previous pieces that are now in the new compacted piece
+                       if (numberToDelete == pendingSendArbitrationRounds.size()) {
+                               pendingSendArbitrationRounds.clear();
+                       } else {
+                               for (int i = 0; i < numberToDelete; i++) {
+                                       pendingSendArbitrationRounds.remove(pendingSendArbitrationRounds.size() - 1);
+                               }
+                       }
 
-               // Reset the table status declared sizes
-               smallestTableStatusSeen = -1;
-               largestTableStatusSeen = -1;
+                       // Add the new compacted into the pending to send list
+                       pendingSendArbitrationRounds.add(lastRound);
 
-               long firstseqnum = newslots[0].getSequenceNumber();
-               if (firstseqnum <= sequencenumber) {
-                       throw new Error("Server Error: Sent older slots!");
+                       // Should reinsert into the commit processor
+                       if (hadCommit && gotNewCommit) {
+                               return true;
+                       }
                }
 
-               SlotIndexer indexer = new SlotIndexer(newslots, buffer);
-               checkHMACChain(indexer, newslots);
+               return false;
+       }
+       // private boolean compactArbitrationData() {
+       //      return false;
+       // }
 
-               HashSet<Long> machineSet = new HashSet<Long>(lastmessagetable.keySet()); //
+       /**
+        * Update all the commits and the committed tables, sets dead the dead transactions
+        */
+       private boolean updateCommittedTable() {
 
-               // initExpectedSize(firstseqnum);
-               for (Slot slot : newslots) {
-                       processSlot(indexer, slot, acceptupdatestolocal, machineSet);
-                       // updateExpectedSize();
+               if (newCommitParts.size() == 0) {
+                       // Nothing new to process
+                       return false;
                }
 
-               /* If there is a gap, check to see if the server sent us everything. */
-               if (firstseqnum != (sequencenumber + 1)) {
+               // Iterate through all the machine Ids that we received new parts for
+               for (Long machineId : newCommitParts.keySet()) {
+                       Map<Pair<Long, Integer>, CommitPart> parts = newCommitParts.get(machineId);
 
-                       // TODO: Check size
-                       checkNumSlots(newslots.length);
-                       if (!machineSet.isEmpty()) {
-                               throw new Error("Missing record for machines: " + machineSet);
-                       }
-               }
+                       // Iterate through all the parts for that machine Id
+                       for (Pair<Long, Integer> partId : parts.keySet()) {
+                               CommitPart part = parts.get(partId);
 
+                               // Get the transaction object for that sequence number
+                               Map<Long, Commit> commitForClientTable = liveCommitsTable.get(part.getMachineId());
 
-               commitNewMaxSize();
+                               if (commitForClientTable == null) {
+                                       // This is the first commit from this device
+                                       commitForClientTable = new HashMap<Long, Commit>();
+                                       liveCommitsTable.put(part.getMachineId(), commitForClientTable);
+                               }
 
-               /* Commit new to slots. */
-               for (Slot slot : newslots) {
-                       buffer.putSlot(slot);
-                       liveslotcount++;
-               }
-               sequencenumber = newslots[newslots.length - 1].getSequenceNumber();
+                               Commit commit = commitForClientTable.get(part.getSequenceNumber());
+
+                               if (commit == null) {
+                                       // This is a new commit that we dont have so make a new one
+                                       commit = new Commit();
 
-               // Process all on key value pairs
-               boolean didCommitOrSpeculate = proccessAllNewCommits();
+                                       // Insert this new commit into the live tables
+                                       commitForClientTable.put(part.getSequenceNumber(), commit);
+                               }
 
-               // Go through all uncommitted transactions and kill the ones that are dead
-               deleteDeadUncommittedTransactions();
+                               // Add that part to the commit
+                               commit.addPartDecode(part);
+                       }
+               }
 
-               // Speculate on key value pairs
-               didCommitOrSpeculate |= createSpeculativeTable();
+               // Clear all the new commits parts in preparation for the next time the server sends slots
+               newCommitParts.clear();
 
-               createPendingTransactionSpeculativeTable(didCommitOrSpeculate);
-       }
+               // If we process a new commit keep track of it for future use
+               boolean didProcessANewCommit = false;
 
-       private boolean proccessAllNewCommits() {
-               // Process only if there are commit
-               if (newCommitMap.keySet().size() == 0) {
-                       return false;
-               }
-               boolean didProcessNewCommit = false;
+               // Process the commits one by one
+               for (Long arbitratorId : liveCommitsTable.keySet()) {
 
-               for (Long arb : newCommitMap.keySet()) {
+                       // Get all the commits for a specific arbitrator
+                       Map<Long, Commit> commitForClientTable = liveCommitsTable.get(arbitratorId);
 
-                       List<Long> commitSeqNums = new ArrayList<Long>(newCommitMap.get(arb).keySet());
+                       // Sort the commits in order
+                       List<Long> commitSequenceNumbers = new ArrayList<Long>(commitForClientTable.keySet());
+                       Collections.sort(commitSequenceNumbers);
 
-                       // Sort from oldest to newest commit
-                       Collections.sort(commitSeqNums);
+                       // Get the last commit seen from this arbitrator
+                       long lastCommitSeenSequenceNumber = -1;
+                       if (lastCommitSeenSequenceNumberByArbitratorTable.get(arbitratorId) != null) {
+                               lastCommitSeenSequenceNumber = lastCommitSeenSequenceNumberByArbitratorTable.get(arbitratorId);
+                       }
 
                        // Go through each new commit one by one
-                       for (Long entrySeqNum : commitSeqNums) {
-                               Commit entry = newCommitMap.get(arb).get(entrySeqNum);
+                       for (int i = 0; i < commitSequenceNumbers.size(); i++) {
+                               Long commitSequenceNumber = commitSequenceNumbers.get(i);
+                               Commit commit = commitForClientTable.get(commitSequenceNumber);
+
+                               // Special processing if a commit is not complete
+                               if (!commit.isComplete()) {
+                                       if (i == (commitSequenceNumbers.size() - 1)) {
+                                               // If there is an incomplete commit and this commit is the latest one seen then this commit cannot be processed and there are no other commits
+                                               break;
+                                       } else {
+                                               // This is a commit that was already dead but parts of it are still in the block chain (not flushed out yet).
+                                               // Delete it and move on
+                                               commit.setDead();
+                                               commitForClientTable.remove(commit.getSequenceNumber());
+                                               continue;
+                                       }
+                               }
 
-                               long lastCommitSeenSeqNum = -1;
-                               if (lastCommitSeenSeqNumMap.get(entry.getTransArbitrator()) != null) {
-                                       lastCommitSeenSeqNum = lastCommitSeenSeqNumMap.get(entry.getTransArbitrator());
+                               // Update the last transaction that was updated if we can
+                               if (commit.getTransactionSequenceNumber() != -1) {
+                                       Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId());
+
+                                       // Update the last transaction sequence number that the arbitrator arbitrated on
+                                       if ((lastTransactionNumber == null) || (lastTransactionNumber < commit.getTransactionSequenceNumber())) {
+                                               lastArbitratedTransactionNumberByArbitratorTable.put(commit.getMachineId(), commit.getTransactionSequenceNumber());
+                                       }
                                }
 
-                               if (entry.getSequenceNumber() <= lastCommitSeenSeqNum) {
-                                       Map<Long, Commit> cm = commitMap.get(arb);
-                                       if (cm == null) {
-                                               cm = new HashMap<Long, Commit>();
+                               // Update the last arbitration data that we have seen so far
+                               if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(commit.getMachineId()) != null) {
+
+                                       long lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(commit.getMachineId());
+                                       if (commit.getSequenceNumber() > lastArbitrationSequenceNumber) {
+                                               // Is larger
+                                               lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(commit.getMachineId(), commit.getSequenceNumber());
                                        }
+                               } else {
+                                       // Never seen any data from this arbitrator so record the first one
+                                       lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(commit.getMachineId(), commit.getSequenceNumber());
+                               }
 
-                                       Commit prevCommit = cm.put(entry.getSequenceNumber(), entry);
-                                       commitMap.put(arb, cm);
+                               // We have already seen this commit before so need to do the full processing on this commit
+                               if (commit.getSequenceNumber() <= lastCommitSeenSequenceNumber) {
 
-                                       if (prevCommit != null) {
-                                               prevCommit.setDead();
+                                       // Update the last transaction that was updated if we can
+                                       if (commit.getTransactionSequenceNumber() != -1) {
+                                               Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId());
 
-                                               for (KeyValue kv : prevCommit.getkeyValueUpdateSet()) {
-                                                       committedMapByKey.put(kv.getKey(), entry);
+                                               // Update the last transaction sequence number that the arbitrator arbitrated on
+                                               if ((lastTransactionNumber == null) || (lastTransactionNumber < commit.getTransactionSequenceNumber())) {
+                                                       lastArbitratedTransactionNumberByArbitratorTable.put(commit.getMachineId(), commit.getTransactionSequenceNumber());
                                                }
                                        }
 
                                        continue;
                                }
 
-                               Set<Commit> commitsToEditSet = new HashSet<Commit>();
+                               // If we got here then this is a brand new commit and needs full processing
 
-                               for (KeyValue kv : entry.getkeyValueUpdateSet()) {
-                                       commitsToEditSet.add(committedMapByKey.get(kv.getKey()));
+                               // Get what commits should be edited, these are the commits that have live values for their keys
+                               Set<Commit> commitsToEdit = new HashSet<Commit>();
+                               for (KeyValue kv : commit.getKeyValueUpdateSet()) {
+                                       commitsToEdit.add(liveCommitsByKeyTable.get(kv.getKey()));
                                }
+                               commitsToEdit.remove(null); // remove null since it could be in this set
 
-                               commitsToEditSet.remove(null);
-
-                               for (Commit prevCommit : commitsToEditSet) {
+                               // Update each previous commit that needs to be updated
+                               for (Commit previousCommit : commitsToEdit) {
 
-                                       Set<KeyValue> deletedKV = prevCommit.updateLiveKeys(entry.getkeyValueUpdateSet());
+                                       // Only bother with live commits (TODO: Maybe remove this check)
+                                       if (previousCommit.isLive()) {
 
-                                       if (!prevCommit.isLive()) {
-                                               Map<Long, Commit> cm = commitMap.get(arb);
+                                               // Update which keys in the old commits are still live
+                                               for (KeyValue kv : commit.getKeyValueUpdateSet()) {
+                                                       previousCommit.invalidateKey(kv.getKey());
+                                               }
 
-                                               // remove it from the map so that it can be set as dead
-                                               if (cm != null) {
-                                                       cm.remove(prevCommit.getSequenceNumber());
-                                                       commitMap.put(arb, cm);
+                                               // if the commit is now dead then remove it
+                                               if (!previousCommit.isLive()) {
+                                                       commitForClientTable.remove(previousCommit);
                                                }
                                        }
                                }
 
-                               // Add the new commit
-                               Map<Long, Commit> cm = commitMap.get(arb);
-                               if (cm == null) {
-                                       cm = new HashMap<Long, Commit>();
-                               }
-                               cm.put(entry.getSequenceNumber(), entry);
-                               commitMap.put(arb, cm);
-
-                               lastCommitSeenSeqNumMap.put(entry.getTransArbitrator(), entry.getSequenceNumber());
-
-                               // set the trans sequence number if we are able to
-                               if (entry.getTransSequenceNumber() != -1) {
-                                       lastCommitSeenTransSeqNumMap.put(entry.getTransArbitrator(), entry.getTransSequenceNumber());
+                               // Update the last seen sequence number from this arbitrator
+                               if (lastCommitSeenSequenceNumberByArbitratorTable.get(commit.getMachineId()) != null) {
+                                       if (commit.getSequenceNumber() > lastCommitSeenSequenceNumberByArbitratorTable.get(commit.getMachineId())) {
+                                               lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber());
+                                       }
+                               } else {
+                                       lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber());
                                }
 
-                               didProcessNewCommit = true;
+                               // We processed a new commit that we havent seen before
+                               didProcessANewCommit = true;
 
-                               // Update the committed table list
-                               for (KeyValue kv : entry.getkeyValueUpdateSet()) {
-                                       IoTString key = kv.getKey();
-                                       commitedTable.put(key, kv);
-                                       committedMapByKey.put(key, entry);
+                               // Update the committed table of keys and which commit is using which key
+                               for (KeyValue kv : commit.getKeyValueUpdateSet()) {
+                                       committedKeyValueTable.put(kv.getKey(), kv);
+                                       liveCommitsByKeyTable.put(kv.getKey(), commit);
                                }
                        }
                }
-               // Clear the new commits storage so we can use it later
-               newCommitMap.clear();
 
-               // go through all saved transactions and update the status of those that can be updated
-               for (Iterator<Map.Entry<Long, TransactionStatus>> i = transactionStatusMap.entrySet().iterator(); i.hasNext();) {
-                       Map.Entry<Long, TransactionStatus> entry = i.next();
-                       long seqnum = entry.getKey();
-                       TransactionStatus status = entry.getValue();
+               return didProcessANewCommit;
+       }
 
-                       if ( status.getSentTransaction() && (lastCommitSeenTransSeqNumMap.get(status.getArbitrator()) != null) && (seqnum <= lastCommitSeenTransSeqNumMap.get(status.getArbitrator()))) {
-                               status.setStatus(TransactionStatus.StatusCommitted);
-                               i.remove();
-                       }
+       /**
+        * Create the speculative table from transactions that are still live and have come from the cloud
+        */
+       private boolean updateSpeculativeTable(boolean didProcessNewCommits) {
+               if (liveTransactionBySequenceNumberTable.keySet().size() == 0) {
+                       // There is nothing to speculate on
+                       return false;
                }
 
-               return didProcessNewCommit;
-       }
-
-       private void deleteDeadUncommittedTransactions() {
-               // Make dead the transactions
-               for (Iterator<Map.Entry<Long, Transaction>> i = uncommittedTransactionsMap.entrySet().iterator(); i.hasNext();) {
-                       Transaction prevtrans = i.next().getValue();
-                       long transArb = prevtrans.getArbitrator();
+               // Create a list of the transaction sequence numbers and sort them from oldest to newest
+               List<Long> transactionSequenceNumbersSorted = new ArrayList<Long>(liveTransactionBySequenceNumberTable.keySet());
+               Collections.sort(transactionSequenceNumbersSorted);
 
-                       Long commitSeqNum = lastCommitSeenTransSeqNumMap.get(transArb);
-                       Long abortSeqNum = lastAbortSeenSeqNumMap.get(transArb);
+               boolean hasGapInTransactionSequenceNumbers = transactionSequenceNumbersSorted.get(0) != oldestTransactionSequenceNumberSpeculatedOn;
 
-                       if (((commitSeqNum != null) && (prevtrans.getSequenceNumber() <= commitSeqNum)) ||
-                               ((abortSeqNum != null) && (prevtrans.getSequenceNumber() <= abortSeqNum))) {
-                               i.remove();
-                               prevtrans.setDead();
-                       }
-               }
-       }
 
-       private boolean createSpeculativeTable() {
-               if (uncommittedTransactionsMap.keySet().size() == 0) {
-                       return false;
-               }
+               if (hasGapInTransactionSequenceNumbers || didProcessNewCommits) {
+                       // If there is a gap in the transaction sequence numbers then there was a commit or an abort of a transaction
+                       // OR there was a new commit (Could be from offline commit) so a redo the speculation from scratch
 
-               Map<IoTString, KeyValue> speculativeTableTmp = new HashMap<IoTString, KeyValue>();
-               List<Long> utSeqNums = new ArrayList<Long>(uncommittedTransactionsMap.keySet());
+                       // Start from scratch
+                       speculatedKeyValueTable.clear();
+                       lastTransactionSequenceNumberSpeculatedOn = -1;
+                       oldestTransactionSequenceNumberSpeculatedOn = -1;
 
-               // Sort from oldest to newest commit
-               Collections.sort(utSeqNums);
+               }
 
-               if (utSeqNums.get(0) > (lastUncommittedTransaction)) {
+               // Remember the front of the transaction list
+               oldestTransactionSequenceNumberSpeculatedOn = transactionSequenceNumbersSorted.get(0);
 
-                       speculativeTable.clear();
-                       lastUncommittedTransaction = -1;
+               // Find where to start arbitration from
+               int startIndex = transactionSequenceNumbersSorted.indexOf(lastTransactionSequenceNumberSpeculatedOn) + 1;
 
-                       for (Long key : utSeqNums) {
-                               Transaction trans = uncommittedTransactionsMap.get(key);
+               if (startIndex >= transactionSequenceNumbersSorted.size()) {
+                       // Make sure we are not out of bounds
+                       return false; // did not speculate
+               }
 
-                               lastUncommittedTransaction = key;
+               Set<Long> incompleteTransactionArbitrator = new HashSet<Long>();
+               boolean didSkip = true;
 
-                               if (trans.evaluateGuard(commitedTable, speculativeTableTmp)) {
-                                       for (KeyValue kv : trans.getkeyValueUpdateSet()) {
-                                               speculativeTableTmp.put(kv.getKey(), kv);
-                                       }
-                               }
+               for (int i = startIndex; i < transactionSequenceNumbersSorted.size(); i++) {
+                       long transactionSequenceNumber = transactionSequenceNumbersSorted.get(i);
+                       Transaction transaction = liveTransactionBySequenceNumberTable.get(transactionSequenceNumber);
 
+                       if (!transaction.isComplete()) {
+                               // If there is an incomplete transaction then there is nothing we can do
+                               // add this transactions arbitrator to the list of arbitrators we should ignore
+                               incompleteTransactionArbitrator.add(transaction.getArbitrator());
+                               didSkip = true;
+                               continue;
                        }
-               } else {
-                       for (Long key : utSeqNums) {
 
-                               if (key <= lastUncommittedTransaction) {
-                                       continue;
-                               }
-
-                               lastUncommittedTransaction = key;
+                       if (incompleteTransactionArbitrator.contains(transaction.getArbitrator())) {
+                               continue;
+                       }
 
-                               Transaction trans = uncommittedTransactionsMap.get(key);
+                       lastTransactionSequenceNumberSpeculatedOn = transactionSequenceNumber;
 
-                               if (trans.evaluateGuard(speculativeTable, speculativeTableTmp)) {
-                                       for (KeyValue kv : trans.getkeyValueUpdateSet()) {
-                                               speculativeTableTmp.put(kv.getKey(), kv);
-                                       }
+                       if (transaction.evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, null)) {
+                               // Guard evaluated to true so update the speculative table
+                               for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
+                                       speculatedKeyValueTable.put(kv.getKey(), kv);
                                }
                        }
                }
 
-               for (IoTString key : speculativeTableTmp.keySet()) {
-                       speculativeTable.put(key, speculativeTableTmp.get(key));
+               if (didSkip) {
+                       // Since there was a skip we need to redo the speculation next time around
+                       lastTransactionSequenceNumberSpeculatedOn = -1;
+                       oldestTransactionSequenceNumberSpeculatedOn = -1;
                }
 
+               // We did some speculation
                return true;
        }
 
-       private void createPendingTransactionSpeculativeTable(boolean didCommitOrSpeculate) {
+       /**
+        * Create the pending transaction speculative table from transactions that are still in the pending transaction buffer
+        */
+       private void updatePendingTransactionSpeculativeTable(boolean didProcessNewCommitsOrSpeculate) {
+               if (pendingTransactionQueue.size() == 0) {
+                       // There is nothing to speculate on
+                       return;
+               }
 
-               if (didCommitOrSpeculate) {
-                       pendingTransSpeculativeTable.clear();
-                       lastSeenPendingTransactionSpeculateIndex = 0;
 
-                       int index = 0;
-                       for (PendingTransaction pt : pendingTransQueue) {
-                               if (pt.evaluateGuard(commitedTable, speculativeTable, pendingTransSpeculativeTable)) {
+               if (didProcessNewCommitsOrSpeculate || (firstPendingTransaction != pendingTransactionQueue.get(0))) {
+                       // need to reset on the pending speculation
+                       lastPendingTransactionSpeculatedOn = null;
+                       firstPendingTransaction = pendingTransactionQueue.get(0);
+                       pendingTransactionSpeculatedKeyValueTable.clear();
+               }
 
-                                       lastSeenPendingTransactionSpeculateIndex = index;
-                                       index++;
+               // Find where to start arbitration from
+               int startIndex = pendingTransactionQueue.indexOf(firstPendingTransaction) + 1;
 
-                                       for (KeyValue kv : pt.getKVUpdates()) {
-                                               pendingTransSpeculativeTable.put(kv.getKey(), kv);
-                                       }
+               if (startIndex >= pendingTransactionQueue.size()) {
+                       // Make sure we are not out of bounds
+                       return;
+               }
 
+               for (int i = startIndex; i < pendingTransactionQueue.size(); i++) {
+                       Transaction transaction = pendingTransactionQueue.get(i);
+
+                       lastPendingTransactionSpeculatedOn = transaction;
+
+                       if (transaction.evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, pendingTransactionSpeculatedKeyValueTable)) {
+                               // Guard evaluated to true so update the speculative table
+                               for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
+                                       pendingTransactionSpeculatedKeyValueTable.put(kv.getKey(), kv);
                                }
                        }
                }
        }
 
-       private int expectedsize, currmaxsize;
+       /**
+        * Set dead and remove from the live transaction tables the transactions that are dead
+        */
+       private void updateLiveTransactionsAndStatus() {
 
-       private void checkNumSlots(int numslots) {
+               // Go through each of the transactions
+               for (Iterator<Map.Entry<Long, Transaction>> iter = liveTransactionBySequenceNumberTable.entrySet().iterator(); iter.hasNext();) {
+                       Transaction transaction = iter.next().getValue();
 
+                       // Check if the transaction is dead
+                       Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(transaction.getArbitrator());
+                       if ((lastTransactionNumber != null) && (lastTransactionNumber >= transaction.getSequenceNumber())) {
 
-               // We only have 1 size so we must have this many slots
-               if (largestTableStatusSeen == smallestTableStatusSeen) {
-                       if (numslots != smallestTableStatusSeen) {
-                               throw new Error("Server Error: Server did not send all slots.  Expected: " + smallestTableStatusSeen + " Received:" + numslots);
-                       }
-               } else {
-                       // We have more than 1
-                       if (numslots < smallestTableStatusSeen) {
-                               throw new Error("Server Error: Server did not send all slots.  Expected at least: " + smallestTableStatusSeen + " Received:" + numslots);
+                               // Set dead the transaction
+                               transaction.setDead();
+
+                               // Remove the transaction from the live table
+                               iter.remove();
+                               liveTransactionByTransactionIdTable.remove(transaction.getId());
                        }
                }
 
-               // if (numslots != expectedsize) {
-               // throw new Error("Server Error: Server did not send all slots.  Expected: " + expectedsize + " Received:" + numslots);
-               // }
-       }
+               // Go through each of the transactions
+               for (Iterator<Map.Entry<Long, TransactionStatus>> iter = outstandingTransactionStatus.entrySet().iterator(); iter.hasNext();) {
+                       TransactionStatus status = iter.next().getValue();
 
-       private void initExpectedSize(long firstsequencenumber) {
-               long prevslots = firstsequencenumber;
-               expectedsize = (prevslots < ((long) numslots)) ? (int) prevslots : numslots;
-               currmaxsize = numslots;
-       }
+                       // Check if the transaction is dead
+                       Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(status.getTransactionArbitrator());
+                       if ((lastTransactionNumber != null) && (lastTransactionNumber >= status.getTransactionSequenceNumber())) {
 
-       private void updateExpectedSize() {
-               expectedsize++;
-               if (expectedsize > currmaxsize) {
-                       expectedsize = currmaxsize;
+                               // Set committed
+                               status.setStatus(TransactionStatus.StatusCommitted);
+
+                               // Remove
+                               iter.remove();
+                       }
                }
        }
 
-       private void updateCurrMaxSize(int newmaxsize) {
-               currmaxsize = newmaxsize;
-       }
+       /**
+        * Process this slot, entry by entry.  Also update the latest message sent by slot
+        */
+       private void processSlot(SlotIndexer indexer, Slot slot, boolean acceptUpdatesToLocal, HashSet<Long> machineSet) {
 
-       private void commitNewMaxSize() {
+               // Update the last message seen
+               updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptUpdatesToLocal, machineSet);
 
-               if (largestTableStatusSeen == -1) {
-                       currmaxsize = numslots;
-               } else {
-                       currmaxsize = largestTableStatusSeen;
-               }
+               // Process each entry in the slot
+               for (Entry entry : slot.getEntries()) {
+                       switch (entry.getType()) {
 
-               if (numslots != currmaxsize) {
-                       buffer.resize(currmaxsize);
-               }
+                       case Entry.TypeCommitPart:
+                               processEntry((CommitPart)entry);
+                               break;
 
-               numslots = currmaxsize;
-               setResizeThreshold();
+                       case Entry.TypeAbort:
+                               processEntry((Abort)entry);
+                               break;
+
+                       case Entry.TypeTransactionPart:
+                               processEntry((TransactionPart)entry);
+                               break;
+
+                       case Entry.TypeNewKey:
+                               processEntry((NewKey)entry);
+                               break;
+
+                       case Entry.TypeLastMessage:
+                               processEntry((LastMessage)entry, machineSet);
+                               break;
+
+                       case Entry.TypeRejectedMessage:
+                               processEntry((RejectedMessage)entry, indexer);
+                               break;
+
+                       case Entry.TypeTableStatus:
+                               processEntry((TableStatus)entry, slot.getSequenceNumber());
+                               break;
+
+                       default:
+                               throw new Error("Unrecognized type: " + entry.getType());
+                       }
+               }
        }
 
+       /**
+        * Update the last message that was sent for a machine Id
+        */
        private void processEntry(LastMessage entry, HashSet<Long> machineSet) {
+               // Update what the last message received by a machine was
                updateLastMessage(entry.getMachineID(), entry.getSequenceNumber(), entry, false, machineSet);
        }
 
+       /**
+        * Add the new key to the arbitrators table and update the set of live new keys (in case of a rescued new key message)
+        */
+       private void processEntry(NewKey entry) {
+
+               // Update the arbitrator table with the new key information
+               arbitratorTable.put(entry.getKey(), entry.getMachineID());
+
+               // Update what the latest live new key is
+               NewKey oldNewKey = liveNewKeyTable.put(entry.getKey(), entry);
+               if (oldNewKey != null) {
+                       // Delete the old new key messages
+                       oldNewKey.setDead();
+               }
+       }
+
+       /**
+        * Process new table status entries and set dead the old ones as new ones come in.
+        * keeps track of the largest and smallest table status seen in this current round
+        * of updating the local copy of the block chain
+        */
+       private void processEntry(TableStatus entry, long seq) {
+               int newNumSlots = entry.getMaxSlots();
+               updateCurrMaxSize(newNumSlots);
+
+               initExpectedSize(seq, newNumSlots);
+
+               if (liveTableStatus != null) {
+                       // We have a larger table status so the old table status is no longer alive
+                       liveTableStatus.setDead();
+               }
+
+               // Make this new table status the latest alive table status
+               liveTableStatus = entry;
+       }
+
+       /**
+        * Check old messages to see if there is a block chain violation. Also
+        */
        private void processEntry(RejectedMessage entry, SlotIndexer indexer) {
-               long oldseqnum = entry.getOldSeqNum();
-               long newseqnum = entry.getNewSeqNum();
+               long oldSeqNum = entry.getOldSeqNum();
+               long newSeqNum = entry.getNewSeqNum();
                boolean isequal = entry.getEqual();
-               long machineid = entry.getMachineID();
-               for (long seqnum = oldseqnum; seqnum <= newseqnum; seqnum++) {
-                       Slot slot = indexer.getSlot(seqnum);
+               long machineId = entry.getMachineID();
+               long seq = entry.getSequenceNumber();
+
+
+               // Check if we have messages that were supposed to be rejected in our local block chain
+               for (long seqNum = oldSeqNum; seqNum <= newSeqNum; seqNum++) {
+
+                       // Get the slot
+                       Slot slot = indexer.getSlot(seqNum);
+
                        if (slot != null) {
-                               long slotmachineid = slot.getMachineID();
-                               if (isequal != (slotmachineid == machineid)) {
-                                       throw new Error("Server Error: Trying to insert rejected message for slot " + seqnum);
+                               // If we have this slot make sure that it was not supposed to be a rejected slot
+
+                               long slotMachineId = slot.getMachineID();
+                               if (isequal != (slotMachineId == machineId)) {
+                                       throw new Error("Server Error: Trying to insert rejected message for slot " + seqNum);
                                }
                        }
                }
 
-               HashSet<Long> watchset = new HashSet<Long>();
-               for (Map.Entry<Long, Pair<Long, Liveness> > lastmsg_entry : lastmessagetable.entrySet()) {
-                       long entry_mid = lastmsg_entry.getKey();
-                       /* We've seen it, don't need to continue to watch.  Our next
-                        * message will implicitly acknowledge it. */
-                       if (entry_mid == localmachineid)
+
+               // Create a list of clients to watch until they see this rejected message entry.
+               HashSet<Long> deviceWatchSet = new HashSet<Long>();
+               for (Map.Entry<Long, Pair<Long, Liveness>> lastMessageEntry : lastMessageTable.entrySet()) {
+
+                       // Machine ID for the last message entry
+                       long lastMessageEntryMachineId = lastMessageEntry.getKey();
+
+                       // We've seen it, don't need to continue to watch.  Our next
+                       // message will implicitly acknowledge it.
+                       if (lastMessageEntryMachineId == localMachineId) {
                                continue;
-                       Pair<Long, Liveness> v = lastmsg_entry.getValue();
-                       long entry_seqn = v.getFirst();
-                       if (entry_seqn < newseqnum) {
-                               addWatchList(entry_mid, entry);
-                               watchset.add(entry_mid);
+                       }
+
+                       Pair<Long, Liveness> lastMessageValue = lastMessageEntry.getValue();
+                       long entrySequenceNumber = lastMessageValue.getFirst();
+
+                       if (entrySequenceNumber < seq) {
+
+                               // Add this rejected message to the set of messages that this machine ID did not see yet
+                               addWatchList(lastMessageEntryMachineId, entry);
+
+                               // This client did not see this rejected message yet so add it to the watch set to monitor
+                               deviceWatchSet.add(lastMessageEntryMachineId);
                        }
                }
-               if (watchset.isEmpty())
+
+               if (deviceWatchSet.isEmpty()) {
+                       // This rejected message has been seen by all the clients so
                        entry.setDead();
-               else
-                       entry.setWatchSet(watchset);
+               } else {
+                       // We need to watch this rejected message
+                       entry.setWatchSet(deviceWatchSet);
+               }
        }
 
-       private void processEntry(NewKey entry) {
-               arbitratorTable.put(entry.getKey(), entry.getMachineID());
+       /**
+        * Check if this abort is live, if not then save it so we can kill it later.
+        * update the last transaction number that was arbitrated on.
+        */
+       private void processEntry(Abort entry) {
 
-               NewKey oldNewKey = newKeyTable.put(entry.getKey(), entry);
 
-               if (oldNewKey != null) {
-                       oldNewKey.setDead();
+               if (entry.getTransactionSequenceNumber() != -1) {
+                       // update the transaction status if it was sent to the server
+                       TransactionStatus status = outstandingTransactionStatus.remove(entry.getTransactionSequenceNumber());
+                       if (status != null) {
+                               status.setStatus(TransactionStatus.StatusAborted);
+                       }
+               }
+
+               // Abort has not been seen by the client it is for yet so we need to keep track of it
+               Abort previouslySeenAbort = liveAbortTable.put(entry.getAbortId(), entry);
+               if (previouslySeenAbort != null) {
+                       previouslySeenAbort.setDead(); // Delete old version of the abort since we got a rescued newer version
                }
-       }
 
-       private void processEntry(Transaction entry) {
+               if (entry.getTransactionArbitrator() == localMachineId) {
+                       liveAbortsGeneratedByLocal.put(entry.getArbitratorLocalSequenceNumber(), entry);
+               }
+
+               if ((entry.getSequenceNumber() != -1) && (lastMessageTable.get(entry.getTransactionMachineId()).getFirst() >= entry.getSequenceNumber())) {
+
+                       // The machine already saw this so it is dead
+                       entry.setDead();
+                       liveAbortTable.remove(entry.getAbortId());
+
+                       if (entry.getTransactionArbitrator() == localMachineId) {
+                               liveAbortsGeneratedByLocal.remove(entry.getArbitratorLocalSequenceNumber());
+                       }
+
+                       return;
+               }
+
+
 
-               long arb = entry.getArbitrator();
-               Long comLast = lastCommitSeenTransSeqNumMap.get(arb);
-               Long abLast = lastAbortSeenSeqNumMap.get(arb);
 
-               Transaction prevTrans = null;
+               // Update the last arbitration data that we have seen so far
+               if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(entry.getTransactionArbitrator()) != null) {
 
-               if ((comLast != null) && (comLast >= entry.getSequenceNumber())) {
-                       prevTrans = uncommittedTransactionsMap.remove(entry.getSequenceNumber());
-               } else if ((abLast != null) && (abLast >= entry.getSequenceNumber())) {
-                       prevTrans = uncommittedTransactionsMap.remove(entry.getSequenceNumber());
+                       long lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(entry.getTransactionArbitrator());
+                       if (entry.getSequenceNumber() > lastArbitrationSequenceNumber) {
+                               // Is larger
+                               lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(entry.getTransactionArbitrator(), entry.getSequenceNumber());
+                       }
                } else {
-                       prevTrans = uncommittedTransactionsMap.put(entry.getSequenceNumber(), entry);
+                       // Never seen any data from this arbitrator so record the first one
+                       lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(entry.getTransactionArbitrator(), entry.getSequenceNumber());
                }
 
-               // Duplicate so delete old copy
-               if (prevTrans != null) {
-                       prevTrans.setDead();
+
+               // Set dead a transaction if we can
+               Transaction transactionToSetDead = liveTransactionByTransactionIdTable.remove(new Pair<Long, Long>(entry.getTransactionMachineId(), entry.getTransactionClientLocalSequenceNumber()));
+               if (transactionToSetDead != null) {
+                       liveTransactionBySequenceNumberTable.remove(transactionToSetDead.getSequenceNumber());
                }
-       }
 
-       private void processEntry(Abort entry) {
-               if (lastmessagetable.get(entry.getMachineID()).getFirst() < entry.getTransSequenceNumber()) {
-                       // Abort has not been seen yet so we need to keep track of it
+               // Update the last transaction sequence number that the arbitrator arbitrated on
+               Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getTransactionArbitrator());
+               if ((lastTransactionNumber == null) || (lastTransactionNumber < entry.getTransactionSequenceNumber())) {
 
-                       Abort prevAbort = abortMap.put(entry.getTransSequenceNumber(), entry);
-                       if (prevAbort != null) {
-                               prevAbort.setDead(); // delete old version of the duplicate
+                       // Is a valid one
+                       if (entry.getTransactionSequenceNumber() != -1) {
+                               lastArbitratedTransactionNumberByArbitratorTable.put(entry.getTransactionArbitrator(), entry.getTransactionSequenceNumber());
                        }
+               }
+       }
 
-                       if ((lastAbortSeenSeqNumMap.get(entry.getTransArbitrator()) != null) &&   (entry.getTransSequenceNumber() > lastAbortSeenSeqNumMap.get(entry.getTransArbitrator()))) {
-                               lastAbortSeenSeqNumMap.put(entry.getTransArbitrator(), entry.getTransSequenceNumber());
-                       }
-               } else {
-                       // The machine already saw this so it is dead
+       /**
+        * Set dead the transaction part if that transaction is dead and keep track of all new parts
+        */
+       private void processEntry(TransactionPart entry) {
+               // Check if we have already seen this transaction and set it dead OR if it is not alive
+               Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getArbitratorId());
+               if ((lastTransactionNumber != null) && (lastTransactionNumber >= entry.getSequenceNumber())) {
+                       // This transaction is dead, it was already committed or aborted
                        entry.setDead();
+                       return;
+               }
+
+               // This part is still alive
+               Map<Pair<Long, Integer>, TransactionPart> transactionPart = newTransactionParts.get(entry.getMachineId());
+
+               if (transactionPart == null) {
+                       // Dont have a table for this machine Id yet so make one
+                       transactionPart = new HashMap<Pair<Long, Integer>, TransactionPart>();
+                       newTransactionParts.put(entry.getMachineId(), transactionPart);
                }
 
-               // Update the status of the transaction and remove it since we are done with this transaction
-               TransactionStatus status = transactionStatusMap.remove(entry.getTransSequenceNumber());
-               if (status != null) {
-                       status.setStatus(TransactionStatus.StatusAborted);
+               // Update the part and set dead ones we have already seen (got a rescued version)
+               TransactionPart previouslySeenPart = transactionPart.put(entry.getPartId(), entry);
+               if (previouslySeenPart != null) {
+                       previouslySeenPart.setDead();
                }
        }
 
-       private void processEntry(Commit entry) {
-               Map<Long, Commit> arbMap = newCommitMap.get(entry.getTransArbitrator());
+       /**
+        * Process new commit entries and save them for future use.  Delete duplicates
+        */
+       private void processEntry(CommitPart entry) {
 
-               if (arbMap == null) {
-                       arbMap = new HashMap<Long, Commit>();
-               }
 
-               Commit prevCommit = arbMap.put(entry.getSequenceNumber(), entry);
-               newCommitMap.put(entry.getTransArbitrator(), arbMap);
+               // Update the last transaction that was updated if we can
+               if (entry.getTransactionSequenceNumber() != -1) {
+                       Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getMachineId());
 
-               if (prevCommit != null) {
-                       prevCommit.setDead();
+                       // Update the last transaction sequence number that the arbitrator arbitrated on
+                       if ((lastTransactionNumber == null) || (lastTransactionNumber < entry.getTransactionSequenceNumber())) {
+                               lastArbitratedTransactionNumberByArbitratorTable.put(entry.getMachineId(), entry.getTransactionSequenceNumber());
+                       }
                }
-       }
 
-       private void processEntry(TableStatus entry) {
-               int newnumslots = entry.getMaxSlots();
-               // updateCurrMaxSize(newnumslots);
-               if (lastTableStatus != null)
-                       lastTableStatus.setDead();
-               lastTableStatus = entry;
 
-               if ((smallestTableStatusSeen == -1) || (newnumslots < smallestTableStatusSeen)) {
-                       smallestTableStatusSeen = newnumslots;
+
+
+               Map<Pair<Long, Integer>, CommitPart> commitPart = newCommitParts.get(entry.getMachineId());
+
+               if (commitPart == null) {
+                       // Don't have a table for this machine Id yet so make one
+                       commitPart = new HashMap<Pair<Long, Integer>, CommitPart>();
+                       newCommitParts.put(entry.getMachineId(), commitPart);
                }
 
-               if ((largestTableStatusSeen == -1) || (newnumslots > largestTableStatusSeen)) {
-                       largestTableStatusSeen = newnumslots;
+               // Update the part and set dead ones we have already seen (got a rescued version)
+               CommitPart previouslySeenPart = commitPart.put(entry.getPartId(), entry);
+               if (previouslySeenPart != null) {
+                       previouslySeenPart.setDead();
                }
        }
 
-       private void addWatchList(long machineid, RejectedMessage entry) {
-               HashSet<RejectedMessage> entries = watchlist.get(machineid);
-               if (entries == null)
-                       watchlist.put(machineid, entries = new HashSet<RejectedMessage>());
-               entries.add(entry);
-       }
+       /**
+        * Update the last message seen table.  Update and set dead the appropriate RejectedMessages as clients see them.
+        * Updates the live aborts, removes those that are dead and sets them dead.
+        * Check that the last message seen is correct and that there is no mismatch of our own last message or that
+        * other clients have not had a rollback on the last message.
+        */
+       private void updateLastMessage(long machineId, long seqNum, Liveness liveness, boolean acceptUpdatesToLocal, HashSet<Long> machineSet) {
+
+               // We have seen this machine ID
+               machineSet.remove(machineId);
 
-       private void updateLastMessage(long machineid, long seqnum, Liveness liveness, boolean acceptupdatestolocal, HashSet<Long> machineSet) {
-               machineSet.remove(machineid);
+               // Get the set of rejected messages that this machine Id is has not seen yet
+               HashSet<RejectedMessage> watchset = rejectedMessageWatchListTable.get(machineId);
 
-               HashSet<RejectedMessage> watchset = watchlist.get(machineid);
+               // If there is a rejected message that this machine Id has not seen yet
                if (watchset != null) {
+
+                       // Go through each rejected message that this machine Id has not seen yet
                        for (Iterator<RejectedMessage> rmit = watchset.iterator(); rmit.hasNext(); ) {
+
                                RejectedMessage rm = rmit.next();
-                               if (rm.getNewSeqNum() <= seqnum) {
-                                       /* Remove it from our watchlist */
+
+                               // If this machine Id has seen this rejected message...
+                               if (rm.getSequenceNumber() <= seqNum) {
+
+                                       // Remove it from our watchlist
                                        rmit.remove();
-                                       /* Decrement machines that need to see this notification */
-                                       rm.removeWatcher(machineid);
+
+                                       // Decrement machines that need to see this notification
+                                       rm.removeWatcher(machineId);
+                               }
+                       }
+               }
+
+               // Set dead the abort
+               for (Iterator<Map.Entry<Pair<Long, Long>, Abort>> i = liveAbortTable.entrySet().iterator(); i.hasNext();) {
+                       Abort abort = i.next().getValue();
+
+                       if ((abort.getTransactionMachineId() == machineId) && (abort.getSequenceNumber() <= seqNum)) {
+                               abort.setDead();
+                               i.remove();
+
+                               if (abort.getTransactionArbitrator() == localMachineId) {
+                                       liveAbortsGeneratedByLocal.remove(abort.getArbitratorLocalSequenceNumber());
                                }
                        }
                }
 
-               if (machineid == localmachineid) {
-                       /* Our own messages are immediately dead. */
+
+
+               if (machineId == localMachineId) {
+                       // Our own messages are immediately dead.
                        if (liveness instanceof LastMessage) {
                                ((LastMessage)liveness).setDead();
                        } else if (liveness instanceof Slot) {
@@ -1516,87 +2681,73 @@ final public class Table {
                        }
                }
 
-               // Set dead the abort
-               for (Iterator<Map.Entry<Long, Abort>> i = abortMap.entrySet().iterator(); i.hasNext();) {
-                       Abort abort = i.next().getValue();
-
-                       if ((abort.getMachineID() == machineid) && (abort.getTransSequenceNumber() <= seqnum)) {
-                               abort.setDead();
-                               i.remove();
-                       }
+               // Get the old last message for this device
+               Pair<Long, Liveness> lastMessageEntry = lastMessageTable.put(machineId, new Pair<Long, Liveness>(seqNum, liveness));
+               if (lastMessageEntry == null) {
+                       // If no last message then there is nothing else to process
+                       return;
                }
 
-               Pair<Long, Liveness> lastmsgentry = lastmessagetable.put(machineid, new Pair<Long, Liveness>(seqnum, liveness));
-               if (lastmsgentry == null)
-                       return;
+               long lastMessageSeqNum = lastMessageEntry.getFirst();
+               Liveness lastEntry = lastMessageEntry.getSecond();
 
-               long lastmsgseqnum = lastmsgentry.getFirst();
-               Liveness lastentry = lastmsgentry.getSecond();
-               if (machineid != localmachineid) {
-                       if (lastentry instanceof LastMessage) {
-                               ((LastMessage)lastentry).setDead();
-                       } else if (lastentry instanceof Slot) {
-                               ((Slot)lastentry).setDead();
+               // If it is not our machine Id since we already set ours to dead
+               if (machineId != localMachineId) {
+                       if (lastEntry instanceof LastMessage) {
+                               ((LastMessage)lastEntry).setDead();
+                       } else if (lastEntry instanceof Slot) {
+                               ((Slot)lastEntry).setDead();
                        } else {
                                throw new Error("Unrecognized type");
                        }
                }
 
-               if (machineid == localmachineid) {
-                       if (lastmsgseqnum != seqnum && !acceptupdatestolocal)
-                               throw new Error("Server Error: Mismatch on local machine sequence number, needed: " +  seqnum + " got: " + lastmsgseqnum);
+               // Make sure the server is not playing any games
+               if (machineId == localMachineId) {
+
+                       if (hadPartialSendToServer) {
+                               // We were not making any updates and we had a machine mismatch
+                               if (lastMessageSeqNum > seqNum && !acceptUpdatesToLocal) {
+                                       throw new Error("Server Error: Mismatch on local machine sequence number, needed at least: " +  lastMessageSeqNum  + " got: " + seqNum);
+                               }
+
+                       } else {
+                               // We were not making any updates and we had a machine mismatch
+                               if (lastMessageSeqNum != seqNum && !acceptUpdatesToLocal) {
+                                       throw new Error("Server Error: Mismatch on local machine sequence number, needed: " +  lastMessageSeqNum + " got: " + seqNum);
+                               }
+                       }
                } else {
-                       if (lastmsgseqnum > seqnum)
+                       if (lastMessageSeqNum > seqNum) {
                                throw new Error("Server Error: Rollback on remote machine sequence number");
+                       }
                }
        }
 
-       private void processSlot(SlotIndexer indexer, Slot slot, boolean acceptupdatestolocal, HashSet<Long> machineSet) {
-               updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptupdatestolocal, machineSet);
-               for (Entry entry : slot.getEntries()) {
-                       switch (entry.getType()) {
-
-                       case Entry.TypeNewKey:
-                               processEntry((NewKey)entry);
-                               break;
-
-                       case Entry.TypeCommit:
-                               processEntry((Commit)entry);
-                               break;
-
-                       case Entry.TypeAbort:
-                               processEntry((Abort)entry);
-                               break;
-
-                       case Entry.TypeTransaction:
-                               processEntry((Transaction)entry);
-                               break;
-
-                       case Entry.TypeLastMessage:
-                               processEntry((LastMessage)entry, machineSet);
-                               break;
-
-                       case Entry.TypeRejectedMessage:
-                               processEntry((RejectedMessage)entry, indexer);
-                               break;
-
-                       case Entry.TypeTableStatus:
-                               processEntry((TableStatus)entry);
-                               break;
-
-                       default:
-                               throw new Error("Unrecognized type: " + entry.getType());
-                       }
+       /**
+        * Add a rejected message entry to the watch set to keep track of which clients have seen that
+        * rejected message entry and which have not.
+        */
+       private void addWatchList(long machineId, RejectedMessage entry) {
+               HashSet<RejectedMessage> entries = rejectedMessageWatchListTable.get(machineId);
+               if (entries == null) {
+                       // There is no set for this machine ID yet so create one
+                       entries = new HashSet<RejectedMessage>();
+                       rejectedMessageWatchListTable.put(machineId, entries);
                }
+               entries.add(entry);
        }
 
-       private void checkHMACChain(SlotIndexer indexer, Slot[] newslots) {
-               for (int i = 0; i < newslots.length; i++) {
-                       Slot currslot = newslots[i];
-                       Slot prevslot = indexer.getSlot(currslot.getSequenceNumber() - 1);
-                       if (prevslot != null &&
-                               !Arrays.equals(prevslot.getHMAC(), currslot.getPrevHMAC()))
-                               throw new Error("Server Error: Invalid HMAC Chain" + currslot + " " + prevslot);
+       /**
+        * Check if the HMAC chain is not violated
+        */
+       private void checkHMACChain(SlotIndexer indexer, Slot[] newSlots) {
+               for (int i = 0; i < newSlots.length; i++) {
+                       Slot currSlot = newSlots[i];
+                       Slot prevSlot = indexer.getSlot(currSlot.getSequenceNumber() - 1);
+                       if (prevSlot != null &&
+                               !Arrays.equals(prevSlot.getHMAC(), currSlot.getPrevHMAC()))
+                               throw new Error("Server Error: Invalid HMAC Chain" + currSlot + " " + prevSlot);
                }
        }
-}
\ No newline at end of file
+}