diff -ru nagios-3.2.0/xdata/xcddefault.c nagios-3.2.0.new/xdata/xcddefault.c
--- nagios-3.2.0/xdata/xcddefault.c	2008-11-30 17:22:59.000000000 +0000
+++ nagios-3.2.0.new/xdata/xcddefault.c	2010-01-28 01:14:20.000000000 +0000
@@ -99,11 +99,6 @@
 
 /* adds a new host comment */
 int xcddefault_add_new_host_comment(int entry_type, char *host_name, time_t entry_time, char *author_name, char *comment_data, int persistent, int source, int expires, time_t expire_time, unsigned long *comment_id){
-
-	/* find the next valid comment id */
-	while(find_host_comment(next_comment_id)!=NULL)
-		next_comment_id++;
-
 	/* add comment to list in memory */
 	add_host_comment(entry_type,host_name,entry_time,author_name,comment_data,next_comment_id,persistent,expires,expire_time,source);
 
@@ -123,11 +118,6 @@
 
 /* adds a new service comment */
 int xcddefault_add_new_service_comment(int entry_type, char *host_name, char *svc_description, time_t entry_time, char *author_name, char *comment_data, int persistent, int source, int expires, time_t expire_time, unsigned long *comment_id){
-
-	/* find the next valid comment id */
-	while(find_service_comment(next_comment_id)!=NULL)
-		next_comment_id++;
-
 	/* add comment to list in memory */
 	add_service_comment(entry_type,host_name,svc_description,entry_time,author_name,comment_data,next_comment_id,persistent,expires,expire_time,source);
 
diff -ru nagios-3.2.0/xdata/xrddefault.c nagios-3.2.0.new/xdata/xrddefault.c
--- nagios-3.2.0/xdata/xrddefault.c	2009-07-31 15:06:49.000000000 +0000
+++ nagios-3.2.0.new/xdata/xrddefault.c	2010-01-28 01:26:57.000000000 +0000
@@ -608,6 +608,7 @@
 int xrddefault_read_state_information(void){
 	char *input=NULL;
 	char *inputbuf=NULL;
+	int inputsize = 0;
 	char *temp_ptr=NULL;
 	mmapfile *thefile;
 	char *host_name=NULL;
@@ -688,14 +689,13 @@
 	contact_host_attribute_mask=retained_contact_host_attribute_mask;
 	contact_service_attribute_mask=retained_contact_service_attribute_mask;
 
+	defer_downtime_sorting = defer_comment_sorting = 1;
+
 	/* read all lines in the retention file */
 	while(1){
 
-		/* free memory */
-		my_free(inputbuf);
-
 		/* read the next line */
-		if((inputbuf=mmap_fgets(thefile))==NULL)
+		if((inputbuf=mmap_fgets_buf(thefile, &inputbuf, &inputsize))==NULL)
 			break;
 
 		input=inputbuf;
@@ -917,6 +917,8 @@
 
 				/* add the comment */
 				add_comment((data_type==XRDDEFAULT_HOSTCOMMENT_DATA)?HOST_COMMENT:SERVICE_COMMENT,entry_type,host_name,service_description,entry_time,author,comment_data,comment_id,persistent,expires,expire_time,source);
+				if (next_comment_id >= comment_id)
+				  next_comment_id = comment_id+1;
 
 				/* delete the comment if necessary */
 				/* it seems a bit backwards to add and then immediately delete the comment, but its necessary to track comment deletions in the event broker */
@@ -1861,6 +1863,11 @@
 	my_free(inputbuf);
 	mmap_fclose(thefile);
 
+	if(sort_downtime()!=OK)
+		return ERROR;
+	if(sort_comments()!=OK)
+		return ERROR;
+
 	if(test_scheduling==TRUE)
 		gettimeofday(&tv[1],NULL);
 
diff -ru nagios-3.2.0/xdata/xsddefault.c nagios-3.2.0.new/xdata/xsddefault.c
--- nagios-3.2.0/xdata/xsddefault.c	2010-01-28 01:29:16.000000000 +0000
+++ nagios-3.2.0.new/xdata/xsddefault.c	2010-01-28 01:31:08.000000000 +0000
@@ -897,6 +897,10 @@
 
 				/* add the comment */
 				add_comment((data_type==XSDDEFAULT_HOSTCOMMENT_DATA)?HOST_COMMENT:SERVICE_COMMENT,entry_type,host_name,service_description,entry_time,author,comment_data,comment_id,persistent,expires,expire_time,source);
+#ifdef NSCORE
+				if (next_comment_id >= comment_id)
+				  next_comment_id = comment_id + 1;
+#endif
 
 				/* free temp memory */
 				my_free(host_name);
