             idInt = parseInt id
             maxId = idInt if idInt > maxId
     
-            mailbox.mailsToBe.create remoteId: idInt, (error, mailToBe) ->
+            mailbox.mailsToBe.create remoteId: idInt, (error, mailToBe) =>
                 if error
-                    server.logout () -> server.emit "error", error
+                    server.logout -> server.emit "error", error
                 else
                     console.log "#{mailToBe.remoteId} id saved successfully"
                     mailsDone++
         
-                    # synchronise - all ids saved to the db
                     if mailsDone is mailsToGo
                         console.log "Finished saving ids to database"
                         console.log "max id = #{maxId}"


Mailbox::doImport = (job, callback) ->

    debug = true
    mailbox = @

    emitOnErr = (server, error) ->
        if error
            server.logout () ->
                console.log error
                server.emit "error", error
  
    @connectImapServer (err, server) =>
        if err
            emitOnErr server, err
        else
            MailToBe.fromMailbox mailbox, (err, mailsToBe) =>
                if err
                    emitOnErr err
                else if not mailsToBe.length
                    console.log 'Nothing to download'
                    server.logout()
                    callback()
                else
                    @loadInbox server, =>
                        loadInboxMails server, mailsToBe
                           
    loadInboxMails = (server, mailsToBe) =>
        mailsToGo = mailsToBe.length
        mailsDone = 0
        fetchOne server, mailsToBe, 0, mailsToGo, mailsDone
        
    fetchOne = (server, mailsToBe, i, mailsToGo, mailsDone) =>
        console.log "fetching one: #{i}/#{mailsToBe.length}"
        
        if i < mailsToBe.length
            
            mailToBe = mailsToBe[i]
            messageFlags = []

            @fetchMessage server, mailToBe, (err) =>
                if err

                priority: mailParsedObject.priority
                text: mailParsedObject.text
                html: mailParsedObject.html
                id_remote_mailbox: remoteId
                headers_raw: JSON.stringify mailParsedObject.headers
                references: mailParsedObject.references or ""
                inReplyTo: mailParsedObject.inReplyTo or ""
