 
                 # step 1 - open one box at random
                 (cb) -> imap.openBox boxIndex[firstboxid].path, cb
-                # step 2 - change flags to newflags
+                # step 2a - remove flags
                 (cb) ->
-                    imap.setFlagsSafe firstuid, oldflags, newflags, cb
+                    if flagsOps.remove.length
+                        imap.delFlags firstuid, flagsOps.remove, cb
+                    else
+                        cb null
+                # step 2b - add flags
+                (cb) ->
+                    if flagsOps.add.length
+                        imap.addFlags firstuid, flagsOps.add, cb
+                    else
+                        cb null
                 # step 3 - copy the message to all addTo
                 (cb) ->
                     paths = boxOps.addTo.map (destId) ->

    # copy the fields
    newmailboxIDs = {}
    newmailboxIDs[boxid] = uid for boxid, uid of @mailboxIDs

    # scan the patch and change the fields
    boxOps = {addTo: [], removeFrom: []}

            delete newmailboxIDs[boxid]
        else throw new Error 'modifying UID is not possible'

    flagsOps = {add: [], remove: []}
    for operation in patch when operation.path.indexOf('/flags/') is 0
        index = parseInt operation.path.substring 7
        if operation.op is 'add'
            flagsOps.add.push operation.value

        else if operation.op is 'remove'
            flagsOps.remove.push operation.value

        else if operation.op is 'replace'
            flagsOps.remove.push @flags[index]
            flagsOps.add.push operation.value

    # create the newflags
    newflags = @flags
    newflags = _.difference newflags, flagsOps.remove
    newflags = _.union newflags, flagsOps.add

    # applyMessageChanges will perform operation in IMAP
    @imap_applyChanges newflags, flagsOps, newmailboxIDs, boxOps, (err, changes) =>
        return callback err if err
        @updateAttributes changes, callback

Message::imap_applyChanges = (newflags, flagsOps, newmailboxIDs, boxOps, callback) ->
    log.debug ".applyChanges", newflags, newmailboxIDs

    oldflags = @flags
