diff --git a/briar-core/src/net/sf/briar/db/JdbcDatabase.java b/briar-core/src/net/sf/briar/db/JdbcDatabase.java
index 9e39557e552827defb6e3c99a389e24ab09f68c2..88b940cfc750cc7eca3722b36783bfa0b79b2778 100644
--- a/briar-core/src/net/sf/briar/db/JdbcDatabase.java
+++ b/briar-core/src/net/sf/briar/db/JdbcDatabase.java
@@ -1387,15 +1387,16 @@ abstract class JdbcDatabase implements Database<Connection> {
 		ResultSet rs = null;
 		try {
 			// Get the local and remote authors
-			String sql = "SELECT l.authorId, l.name, l.publicKey,"
-					+ " r.authorId, r.name, r.publicKey"
-					+ " FROM localAuthors AS l"
-					+ " JOIN contacts AS r"
-					+ " ON l.authorId = r.localAuthorId"
+			String sql = "SELECT la.authorId, la.name, la.publicKey,"
+					+ " c.authorId, c.name, c.publicKey"
+					+ " FROM localAuthors AS la"
+					+ " JOIN contacts AS c"
+					+ " ON la.authorId = c.localAuthorId"
 					+ " WHERE contactId = ?";
 			ps = txn.prepareStatement(sql);
 			ps.setInt(1, c.getInt());
 			rs = ps.executeQuery();
+			if(!rs.next()) throw new DbException();
 			AuthorId localId = new AuthorId(rs.getBytes(1));
 			String localName = rs.getString(2);
 			byte[] localKey = rs.getBytes(3);
@@ -1404,6 +1405,7 @@ abstract class JdbcDatabase implements Database<Connection> {
 			String remoteName = rs.getString(5);
 			byte[] remoteKey = rs.getBytes(6);
 			Author remoteAuthor = new Author(remoteId, remoteName, remoteKey);
+			if(rs.next()) throw new DbException();
 			// Get the message headers
 			sql = "SELECT messageId, parentId, m.groupId, contentType,"
 					+ " timestamp, incoming, read"
@@ -1412,7 +1414,7 @@ abstract class JdbcDatabase implements Database<Connection> {
 					+ " ON m.groupId = g.groupId"
 					+ " JOIN groupVisibilities AS gv"
 					+ " ON m.groupId = gv.groupId"
-					+ " WHERE gv.contactId = ?"
+					+ " WHERE contactId = ?"
 					+ " AND inbox = TRUE";
 			ps = txn.prepareStatement(sql);
 			ps.setInt(1, c.getInt());
diff --git a/briar-tests/src/net/sf/briar/db/H2DatabaseTest.java b/briar-tests/src/net/sf/briar/db/H2DatabaseTest.java
index e4e3e1959f60eb362b52f66c1c4f1e3fc953ca54..044f1f9ac037f5e2464e6687440316518407c133 100644
--- a/briar-tests/src/net/sf/briar/db/H2DatabaseTest.java
+++ b/briar-tests/src/net/sf/briar/db/H2DatabaseTest.java
@@ -1628,6 +1628,39 @@ public class H2DatabaseTest extends BriarTestCase {
 		db.close();
 	}
 
+	@Test
+	public void testGetInboxMessageHeaders() throws Exception {
+		Database<Connection> db = open(false);
+		Connection txn = db.startTransaction();
+
+		// Add a contact and an inbox group - no headers should be returned
+		db.addLocalAuthor(txn, localAuthor);
+		assertEquals(contactId, db.addContact(txn, author, localAuthorId));
+		Group inbox = new Group(groupId, "Group", new byte[GROUP_SALT_LENGTH],
+				true);
+		db.addGroup(txn, inbox);
+		db.setInboxGroup(txn, contactId, inbox);
+		assertEquals(Collections.emptyList(),
+				db.getInboxMessageHeaders(txn, contactId));
+
+		// Add a message to the inbox group - the header should be returned
+		db.addMessage(txn, message, false);
+		Collection<MessageHeader> headers =
+				db.getInboxMessageHeaders(txn, contactId);
+		assertEquals(1, headers.size());
+		MessageHeader header = headers.iterator().next();
+		assertEquals(messageId, header.getId());
+		assertNull(header.getParent());
+		assertEquals(groupId, header.getGroupId());
+		assertEquals(localAuthor, header.getAuthor());
+		assertEquals(contentType, header.getContentType());
+		assertEquals(timestamp, header.getTimestamp());
+		assertFalse(header.isRead());
+
+		db.commitTransaction(txn);
+		db.close();
+	}
+
 	@Test
 	public void testExceptionHandling() throws Exception {
 		Database<Connection> db = open(false);