 +  NSDictionary *d;
 +  NSCalendarDate *envDate;
 +
-+  result = [[[NSMutableDictionary alloc] init] autorelease];
++  result = [NSMutableDictionary dictionary];
 +  [result setObject: [NSNumber numberWithBool: NO]  forKey: @"result"];
 +
 +  // _sortSpec: [REVERSE] {DATE,FROM,SUBJECT}
 +  d = [self searchWithQualifier: _qual];
 +    
-+  if ((d = [d objectForKey: @"RawResponse"]))
-+    {
-+      NSMutableDictionary *dict;
-+      NSArray *a, *s_a;
-+      BOOL b;
-+      int i;
-+
-+      a = [d objectForKey: @"search"];      
-+      d = [self fetchUids: a  parts: [NSArray arrayWithObject: @"ENVELOPE"]];
++  if ((d = [d objectForKey: @"RawResponse"])) {
++    NSMutableDictionary *dict;
++    NSArray *a, *s_a;
++    BOOL b;
++    int i;
++
++    a = [d objectForKey: @"search"];
++    if ([a isNotEmpty]) {
++      d = [self fetchUids: a parts: [NSArray arrayWithObject: @"ENVELOPE"]];
 +      a = [d objectForKey: @"fetch"];
-+     
-+
-+      dict = [[[NSMutableDictionary alloc] init] autorelease];
++
++      dict = [NSMutableDictionary dictionary];
 +      b = YES;
 +
-+      for (i = 0; i < [a count]; i++) 
-+	{
-+	  NGImap4Envelope *env;
-+	  id o, uid, s;
-+
-+	  o = [a objectAtIndex: i];
-+	  env = [o objectForKey: @"envelope"];
-+	  uid = [o objectForKey: @"uid"];
-+
-+	  if ([_sortSpec rangeOfString: @"SUBJECT"].length)
-+	    {
-+	      s = [env subject];	      
-+	      if ([s isKindOfClass: [NSData class]])
-+		s = [[[NSString alloc] initWithData: s  encoding: NSUTF8StringEncoding] autorelease];
++      for (i = 0; i < [a count]; i++) {
++        NGImap4Envelope *env;
++        id o, uid, s;
++
++        o = [a objectAtIndex: i];
++        env = [o objectForKey: @"envelope"];
++        uid = [o objectForKey: @"uid"];
++
++        if ([_sortSpec rangeOfString: @"SUBJECT"].length) {
++          s = [env subject];
++          if ([s isKindOfClass: [NSData class]])
++            s = [[[NSString alloc] initWithData: s  encoding: NSUTF8StringEncoding] autorelease];
 +	      
-+	      [dict setObject: (s != nil ? s : (id)@"")  forKey: uid];
-+	    }
-+	  else if ([_sortSpec rangeOfString: @"FROM"].length)
-+	    {
-+	      s =  [[[env from] lastObject] email];
-+	      [dict setObject: (s != nil ? s : (id)@"")  forKey: uid];
-+	    }
-+	  else
-+	    {
-+ 	      envDate = [env date];
-+ 	      if (!envDate)
-+ 		envDate = [NSCalendarDate date];
-+	      [dict setObject: envDate forKey: uid];
-+	      b = NO;
-+	    }
-+	}
++          [dict setObject: (s != nil ? s : (id)@"")  forKey: uid];
++        }
++        else if ([_sortSpec rangeOfString: @"FROM"].length) {
++          s =  [[[env from] lastObject] email];
++          [dict setObject: (s != nil ? s : (id)@"")  forKey: uid];
++        }
++        else {
++          envDate = [env date];
++          if (!envDate)
++            envDate = [NSCalendarDate date];
++          [dict setObject: envDate forKey: uid];
++          b = NO;
++        }
++      }
 +      
 +      if (b)
 +      	s_a = [dict keysSortedByValueUsingSelector: @selector(caseInsensitiveCompare:)];
 +      else
 +      	s_a = [dict keysSortedByValueUsingSelector: @selector(compare:)];
 +
-+      if ([_sortSpec rangeOfString: @"REVERSE"].length)
-+	{
-+	  s_a = [[s_a reverseObjectEnumerator] allObjects];
-+	}
++      if ([_sortSpec rangeOfString: @"REVERSE"].length)	{
++        s_a = [[s_a reverseObjectEnumerator] allObjects];
++      }
 +      
-+      [result setObject: [NSNumber numberWithBool: YES]  forKey: @"result"];
-+      [result setObject: s_a  forKey: @"sort"];
 +    }
++    else {
++      s_a = [NSArray array];
++    }
++    [result setObject: [NSNumber numberWithBool: YES]  forKey: @"result"];
++    [result setObject: s_a  forKey: @"sort"];
++  }
 +
 +  return result;
 +}

 }
 
 - (NSString *)_imapFolder2Folder:(NSString *)_folder {
@@ -1442,10 +1550,16 @@
       return nil;
   }
   

   return [self->normer normalizeListRightsResponse:[self processCommand:cmd]];
 }
 
@@ -1187,12 +1216,86 @@
   if ((_folder = [self _folder2ImapFolder:_folder]) == nil)
     return nil;
   
