Index: /trunk/downstream.go
===================================================================
--- /trunk/downstream.go	(revision 484)
+++ /trunk/downstream.go	(revision 485)
@@ -362,10 +362,5 @@
 	}
 
-	delivered := network.delivered.Value(entity)
-	if delivered == nil {
-		return
-	}
-
-	delivered[dc.clientName] = id
+	network.delivered.StoreID(entity, dc.clientName, id)
 }
 
@@ -998,22 +993,25 @@
 		})
 		if firstClient {
-			dc.sendNetworkBacklog(net)
+			net.delivered.ForEachTarget(func(target string) {
+				dc.sendTargetBacklog(net, target)
+			})
 		}
 
 		// Fast-forward history to last message
-		for targetCM, entry := range net.delivered.innerMap {
-			delivered := entry.value.(deliveredClientMap)
-			ch := net.channels.Value(targetCM)
+		net.delivered.ForEachTarget(func(target string) {
+			ch := net.channels.Value(target)
 			if ch != nil && ch.Detached {
-				continue
-			}
-
+				return
+			}
+
+			targetCM := net.casemap(target)
 			lastID, err := dc.user.msgStore.LastMsgID(net, targetCM, time.Now())
 			if err != nil {
 				dc.logger.Printf("failed to get last message ID: %v", err)
-				continue
-			}
-			delivered[dc.clientName] = lastID
-		}
+				return
+			}
+
+			net.delivered.StoreID(target, dc.clientName, lastID)
+		})
 	})
 
@@ -1035,11 +1033,4 @@
 }
 
-func (dc *downstreamConn) sendNetworkBacklog(net *network) {
-	for _, entry := range net.delivered.innerMap {
-		target := entry.originalKey
-		dc.sendTargetBacklog(net, target)
-	}
-}
-
 func (dc *downstreamConn) sendTargetBacklog(net *network, target string) {
 	if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
@@ -1049,10 +1040,7 @@
 		return
 	}
-	delivered := net.delivered.Value(target)
-	if delivered == nil {
-		return
-	}
-	lastDelivered, ok := delivered[dc.clientName]
-	if !ok {
+
+	lastDelivered := net.delivered.LoadID(target, dc.clientName)
+	if lastDelivered == "" {
 		return
 	}
Index: /trunk/upstream.go
===================================================================
--- /trunk/upstream.go	(revision 484)
+++ /trunk/upstream.go	(revision 485)
@@ -1741,7 +1741,7 @@
 	}
 
-	delivered := uc.network.delivered.Value(entity)
 	entityCM := uc.network.casemap(entity)
-	if delivered == nil {
+
+	if !uc.network.delivered.HasTarget(entity) {
 		// This is the first message we receive from this target. Save the last
 		// message ID in delivery receipts, so that we can send the new message
@@ -1753,9 +1753,6 @@
 		}
 
-		delivered = make(deliveredClientMap)
-		uc.network.delivered.SetValue(entity, delivered)
-
 		for clientName, _ := range uc.user.clients {
-			delivered[clientName] = lastID
+			uc.network.delivered.StoreID(entity, clientName, lastID)
 		}
 	}
Index: /trunk/user.go
===================================================================
--- /trunk/user.go	(revision 484)
+++ /trunk/user.go	(revision 485)
@@ -58,4 +58,39 @@
 type deliveredClientMap map[string]string // client name -> msg ID
 
+type deliveredStore struct {
+	m deliveredCasemapMap
+}
+
+func newDeliveredStore() deliveredStore {
+	return deliveredStore{deliveredCasemapMap{newCasemapMap(0)}}
+}
+
+func (ds deliveredStore) HasTarget(target string) bool {
+	return ds.m.Value(target) != nil
+}
+
+func (ds deliveredStore) LoadID(target, clientName string) string {
+	clients := ds.m.Value(target)
+	if clients == nil {
+		return ""
+	}
+	return clients[clientName]
+}
+
+func (ds deliveredStore) StoreID(target, clientName, msgID string) {
+	clients := ds.m.Value(target)
+	if clients == nil {
+		clients = make(deliveredClientMap)
+		ds.m.SetValue(target, clients)
+	}
+	clients[clientName] = msgID
+}
+
+func (ds deliveredStore) ForEachTarget(f func(target string)) {
+	for _, entry := range ds.m.innerMap {
+		f(entry.originalKey)
+	}
+}
+
 type network struct {
 	Network
@@ -65,5 +100,5 @@
 	conn      *upstreamConn
 	channels  channelCasemapMap
-	delivered deliveredCasemapMap
+	delivered deliveredStore
 	lastError error
 	casemap   casemapping
@@ -82,5 +117,5 @@
 		stopped:   make(chan struct{}),
 		channels:  m,
-		delivered: deliveredCasemapMap{newCasemapMap(0)},
+		delivered: newDeliveredStore(),
 		casemap:   casemapRFC1459,
 	}
@@ -254,5 +289,5 @@
 	net.casemap = newCasemap
 	net.channels.SetCasemapping(newCasemap)
-	net.delivered.SetCasemapping(newCasemap)
+	net.delivered.m.SetCasemapping(newCasemap)
 	if net.conn != nil {
 		net.conn.channels.SetCasemapping(newCasemap)
