         }
     }
     
-    func earliestArchivedMessage(contactBareJidStr:NSString, streamBareJidStr:NSString)-> XMPPMessageArchiveManagement_Message_CoreDataObject? {
+    func oldestArchivedMessage(contactBareJidStr:NSString, streamBareJidStr:NSString)-> XMPPMessageArchiveManagement_Message_CoreDataObject? {
         return archivedMessage(contactBareJidStr, streamBareJidStr: streamBareJidStr, ascending: true)
     }
     

        
        let archive = connection.getArchive()
        
        let latest = latestArchivedMessage(to.xmppJid.bare(), streamBareJidStr: from.xmppJid.bare())
        
        if(latest == nil) {
            archive.mamQueryWith(to.xmppJid, andStart: NSDate(), andEnd: nil, andResultSet: XMPPResultSet(max: 100))
        }else{
            archive.mamQueryWith(to.xmppJid, andStart: latest!.timestamp, andEnd: nil, andResultSet: XMPPResultSet(max: 100))
        }
    }
    

        return archivedMessage(contactBareJidStr, streamBareJidStr: streamBareJidStr, ascending: false)
    }
    
    private func mamQueryLatest100(archive:XMPPMessageArchiveManagement) {
        archive.mamQueryWith(to.xmppJid, andStart: nil, andEnd: NSDate(), andResultSet: XMPPResultSet(max: 100, before: "" ));
    }
    
    private func mamQueryCatchup(archive:XMPPMessageArchiveManagement, latestArchived:XMPPMessageArchiveManagement_Message_CoreDataObject ) {
        archive.mamQueryWith(to.xmppJid, andStart: latestArchived.timestamp, andEnd: NSDate(), andResultSet: nil)
    }
    
    private func mamQueryNextHistory(archive:XMPPMessageArchiveManagement, oldestArchived:XMPPMessageArchiveManagement_Message_CoreDataObject){
        archive.mamQueryWith(to.xmppJid, andStart: oldestArchived.timestamp, andEnd: nil, andResultSet: XMPPResultSet(max: 100))
    }
    
    func loadInitialArchive() {
        
        let archive = connection.getArchive()

        //TODO ignore local messages
        
        let latestArchived = latestArchivedMessage(to.xmppJid.bare(), streamBareJidStr: from.xmppJid.bare())
        
        if(latestArchived == nil){
            mamQueryLatest100(archive)
        }else{
            mamQueryCatchup(archive, latestArchived:latestArchived!)
        }
    }
    
