diff --git a/components/net/sf/briar/db/DatabaseComponentImpl.java b/components/net/sf/briar/db/DatabaseComponentImpl.java
index f864b889781ec02f4807dcb7282fa3b96904ae41..e08425e7deaa78b122acff3d5548612cdff349b5 100644
--- a/components/net/sf/briar/db/DatabaseComponentImpl.java
+++ b/components/net/sf/briar/db/DatabaseComponentImpl.java
@@ -271,15 +271,21 @@ DatabaseCleaner.Callback {
 	 */
 	private int updateAncestorSendability(T txn, MessageId m, boolean increment)
 	throws DbException {
+		GroupId group = db.getGroup(txn, m);
 		int affected = 0;
 		boolean changed = true;
 		while(changed) {
+			// Stop if the message has no parent
 			MessageId parent = db.getParent(txn, m);
 			if(parent == null) break;
+			// Stop if the parent isn't in the database
 			if(!db.containsMessage(txn, parent)) break;
-			if(!db.getGroup(txn, m).equals(db.getGroup(txn, parent))) break;
-			Integer parentSendability = db.getSendability(txn, parent);
-			assert parentSendability != null;
+			// Stop if the message and the parent aren't in the same group
+			assert group != null;
+			GroupId parentGroup = db.getGroup(txn, parent);
+			if(!group.equals(parentGroup)) break;
+			// Increment or decrement the parent's sendability
+			int parentSendability = db.getSendability(txn, parent);
 			if(increment) {
 				parentSendability++;
 				changed = parentSendability == 1;
@@ -291,7 +297,9 @@ DatabaseCleaner.Callback {
 				if(changed) affected++;
 			}
 			db.setSendability(txn, parent, parentSendability);
+			// Move on to the parent's parent
 			m = parent;
+			group = parentGroup;
 		}
 		return affected;
 	}
diff --git a/test/net/sf/briar/db/DatabaseComponentImplTest.java b/test/net/sf/briar/db/DatabaseComponentImplTest.java
index ea17f97171549bd83f5eaf544201f1992a9127fb..3c351f3a11beffb149c97f29f4e397a2cad08a66 100644
--- a/test/net/sf/briar/db/DatabaseComponentImplTest.java
+++ b/test/net/sf/briar/db/DatabaseComponentImplTest.java
@@ -105,6 +105,8 @@ public class DatabaseComponentImplTest extends DatabaseComponentTest {
 			will(returnValue(Collections.singleton(messageId)));
 			oneOf(database).getSendability(txn, messageId);
 			will(returnValue(1));
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(null));
 			oneOf(database).removeMessage(txn, messageId);
diff --git a/test/net/sf/briar/db/DatabaseComponentTest.java b/test/net/sf/briar/db/DatabaseComponentTest.java
index 18425720cbdba5628d63c6d0417514a3d5a5c2aa..886b2d7da4eafb8e456b92926a11e104c7b2e4a4 100644
--- a/test/net/sf/briar/db/DatabaseComponentTest.java
+++ b/test/net/sf/briar/db/DatabaseComponentTest.java
@@ -199,6 +199,8 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// Backward inclusion stops when the message has no parent
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(null));
 			oneOf(database).commitTransaction(txn);
@@ -228,6 +230,8 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			// The parent isn't in the DB
@@ -262,13 +266,13 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists and is in the database
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			oneOf(database).containsMessage(txn, parentId);
 			will(returnValue(true));
 			// The parent is in a different group
-			oneOf(database).getGroup(txn, messageId);
-			will(returnValue(groupId));
 			oneOf(database).getGroup(txn, parentId);
 			will(returnValue(groupId1));
 			oneOf(database).commitTransaction(txn);
@@ -298,13 +302,13 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists and is in the database
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			oneOf(database).containsMessage(txn, parentId);
 			will(returnValue(true));
 			// The parent is a private message
-			oneOf(database).getGroup(txn, messageId);
-			will(returnValue(groupId));
 			oneOf(database).getGroup(txn, parentId);
 			will(returnValue(null));
 			oneOf(database).commitTransaction(txn);
@@ -335,12 +339,12 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists, is in the DB, and is in the same group
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			oneOf(database).containsMessage(txn, parentId);
 			will(returnValue(true));
-			oneOf(database).getGroup(txn, messageId);
-			will(returnValue(groupId));
 			oneOf(database).getGroup(txn, parentId);
 			will(returnValue(groupId));
 			// The parent is already sendable
@@ -375,12 +379,12 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(0));
 			oneOf(database).setSendability(txn, messageId, 1);
 			// The parent exists, is in the DB, and is in the same group
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(parentId));
 			oneOf(database).containsMessage(txn, parentId);
 			will(returnValue(true));
-			oneOf(database).getGroup(txn, messageId);
-			will(returnValue(groupId));
 			oneOf(database).getGroup(txn, parentId);
 			will(returnValue(groupId));
 			// The parent is not already sendable
@@ -500,6 +504,8 @@ public abstract class DatabaseComponentTest extends TestCase {
 			will(returnValue(2));
 			oneOf(database).setSendability(txn, messageId, 3);
 			// The sendability of the message's ancestors should be updated
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(null));
 			oneOf(database).commitTransaction(txn);
@@ -1134,12 +1140,14 @@ public abstract class DatabaseComponentTest extends TestCase {
 			// Set the status to NEW for all other contacts (there are none)
 			oneOf(database).getContacts(txn);
 			will(returnValue(Collections.singletonList(contactId)));
-			// Calculate the sendability -ancestors are updated
+			// Calculate the sendability - ancestors are updated
 			oneOf(database).getRating(txn, authorId);
 			will(returnValue(Rating.GOOD));
 			oneOf(database).getNumberOfSendableChildren(txn, messageId);
 			will(returnValue(1));
 			oneOf(database).setSendability(txn, messageId, 2);
+			oneOf(database).getGroup(txn, messageId);
+			will(returnValue(groupId));
 			oneOf(database).getParent(txn, messageId);
 			will(returnValue(null));
 			// The batch needs to be acknowledged