diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index 9e3c5c9a0203c05051eb55e9c13baf2a849962b6..304a3561c3fd15cffb30469d162719124f1a3481 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -1696,10 +1696,13 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
 			return None
 
 	def delete_session(self, jid, thread_id):
-		del self.sessions[jid][thread_id]
-
-		if not self.sessions[jid]:
-			del self.sessions[jid]
+		try:
+			del self.sessions[jid][thread_id]
+			
+			if not self.sessions[jid]:
+				del self.sessions[jid]
+		except KeyError
+			print "jid %s should have been in %s, but it wasn't. missing session?" % (repr(jid), repr(self.sessions.keys())
 
 	def move_session(self, original_jid, thread_id, to_resource):
 		'''moves a session to another resource.'''
@@ -1722,9 +1725,12 @@ returns the session that we last sent a message to.'''
 		
 		sessions_with_jid = self.sessions[jid].values()
 		no_threadid_sessions = filter(lambda s: not s.received_thread_id, sessions_with_jid)
-		no_threadid_sessions.sort(key=lambda s: s.last_send)
 
-		return no_threadid_sessions[-1]
+		if no_threadid_sessions:
+			no_threadid_sessions.sort(key=lambda s: s.last_send)
+			return no_threadid_sessions[-1]
+		else:
+			return None
 
 	def make_new_session(self, jid, thread_id = None, type = 'chat'):
 		sess = EncryptedStanzaSession(self, common.xmpp.JID(jid), thread_id, type)
diff --git a/src/message_control.py b/src/message_control.py
index 4489599fb6e67b53b3d2af5aab447e8a36e1f1fc..2914f952bd90d53c05beed1cf8996a5d840b0662 100644
--- a/src/message_control.py
+++ b/src/message_control.py
@@ -117,7 +117,7 @@ class MessageControl:
 
 		if self.session:
 			print "starting a new session, dropping the old one!"
-			gajim.connections[self.account].delete_session(self.contact.get_full_jid(), self.session.thread_id)
+			gajim.connections[self.account].delete_session(self.session.jid, self.session.thread_id)
 
 		self.session = session