diff --git a/camel/camel-db.c b/camel/camel-db.c
index 8fa7049..1966914 100644
--- a/camel/camel-db.c
+++ b/camel/camel-db.c
@@ -358,6 +358,7 @@ struct _CamelDBPrivate {
 	GTimer *timer;
 	GStaticRWLock rwlock;
 	gchar *file_name;
+	gboolean transaction_is_on;
 };
 
 /**
@@ -523,6 +524,8 @@ camel_db_begin_transaction (CamelDB *cdb, CamelException *ex)
 	WRITER_LOCK (cdb);
 	STARTTS("BEGIN");
 
+	cdb->priv->transaction_is_on = TRUE;
+
 	return (cdb_sql_exec (cdb->db, "BEGIN", NULL, NULL, NULL));
 }
 
@@ -534,6 +537,8 @@ camel_db_end_transaction (CamelDB *cdb, CamelException *ex)
 		return -1;
 
 	ret = cdb_sql_exec (cdb->db, "COMMIT", NULL, NULL, ex);
+	cdb->priv->transaction_is_on = FALSE;
+
 	ENDTS;
 
 	WRITER_UNLOCK (cdb);
@@ -548,6 +553,7 @@ camel_db_abort_transaction (CamelDB *cdb, CamelException *ex)
 	gint ret;
 
 	ret = cdb_sql_exec (cdb->db, "ROLLBACK", NULL, NULL, ex);
+	cdb->priv->transaction_is_on = FALSE;
 
 	WRITER_UNLOCK (cdb);
 	CAMEL_DB_RELEASE_SQLITE_MEMORY;
@@ -561,6 +567,8 @@ camel_db_add_to_transaction (CamelDB *cdb, const gchar *stmt, CamelException *ex
 	if (!cdb)
 		return -1;
 
+	g_assert(cdb->priv->transaction_is_on == TRUE);
+
 	return (cdb_sql_exec (cdb->db, stmt, NULL, NULL, ex));
 }
 
@@ -1263,23 +1271,25 @@ camel_db_write_folder_version (CamelDB *cdb, const gchar *folder_name, gint old_
 }
 
 static gint
+read_version_callback(gpointer ref, gint ncol, gchar **cols, gchar **name)
+{
+	gint *version = (gint *) ref;
+
+	if (cols[0])
+		*version =strtoul(cols [0], NULL, 10);
+
+	return 0;
+}
+
+static gint
 camel_db_get_folder_version (CamelDB *cdb, const gchar *folder_name, CamelException *ex)
 {
 	gint version = -1, ret;
 	gchar *query;
-	sqlite3_stmt *stmt = NULL;
 
 	query = sqlite3_mprintf ("SELECT version FROM '%q_version'", folder_name);
 
-	ret = sqlite3_prepare_v2 (cdb->db, query, -1, &stmt, NULL);
-
-	if (ret == SQLITE_OK)
-		ret = sqlite3_step (stmt);
-	if (ret == SQLITE_ROW || ret == SQLITE_OK)
-		version = sqlite3_column_int (stmt, 0);
-
-	sqlite3_finalize (stmt);
-
+	ret = camel_db_select(cdb, query, read_version_callback, &version, ex);
 	sqlite3_free (query);
 
 	return version;
@@ -1291,19 +1301,44 @@ camel_db_prepare_message_info_table (CamelDB *cdb, const gchar *folder_name, Cam
 	gint ret, current_version;
 
 	/* Make sure we have the table already */
+	camel_db_begin_transaction (cdb, ex);
 	ret = camel_db_create_message_info_table (cdb, folder_name, ex);
+	if(ex && camel_exception_is_set(ex))
+		goto exit;
+
+	camel_db_end_transaction (cdb, ex);
 
 	/* Migration stage zero: version fetch */
 	current_version = camel_db_get_folder_version (cdb, folder_name, ex);
 
 	/* Migration stage one: storing the old data if necessary */
+	camel_db_begin_transaction (cdb, ex);
 	ret = camel_db_migrate_folder_prepare (cdb, folder_name, current_version, ex);
+	if(ex && camel_exception_is_set(ex))
+		goto exit;
 
 	/* Migration stage two: rewriting the old data if necessary */
 	ret = camel_db_migrate_folder_recreate (cdb, folder_name, current_version, ex);
+	if(ex && camel_exception_is_set(ex))
+		goto exit;
 
 	/* Final step: (over)write the current version label */
 	ret = camel_db_write_folder_version (cdb, folder_name, current_version, ex);
+	if(ex && camel_exception_is_set(ex))
+		goto exit;
+
+	camel_db_end_transaction(cdb, ex);
+
+exit:
+	if (ex && camel_exception_is_set(ex)) {
+		const gchar *exception = camel_exception_get_description (ex);
+		if (cdb->priv->transaction_is_on)
+			camel_db_abort_transaction(cdb, NULL);
+
+                if (strncmp(exception, "no such table", 13) == 0) {
+                        camel_exception_clear (ex); /* Suppress no such table */
+                }
+	}
 
 	return ret;
 }
diff --git a/camel/camel-folder-summary.c b/camel/camel-folder-summary.c
index d691c49..a7c26a9 100644
--- a/camel/camel-folder-summary.c
+++ b/camel/camel-folder-summary.c
@@ -985,6 +985,8 @@ camel_folder_summary_reload_from_db (CamelFolderSummary *s, CamelException *ex)
 	 * load better. */
 	d(printf ("\ncamel_folder_summary_reload_from_db called \n"));
 
+	CAMEL_SUMMARY_LOCK (s, summary_lock);
+
 	folder_name = s->folder->full_name;
 	cdb = s->folder->parent_store->cdb_r;
 
@@ -999,6 +1001,8 @@ camel_folder_summary_reload_from_db (CamelFolderSummary *s, CamelException *ex)
 	if (!g_getenv("CAMEL_FREE_INFOS") && !s->timeout_handle)
 		s->timeout_handle = g_timeout_add_seconds (SUMMARY_CACHE_DROP, (GSourceFunc) cfs_try_release_memory, s);
 
+	CAMEL_SUMMARY_UNLOCK (s, summary_lock);
+
 	if (_PRIVATE(s)->need_preview) {
 		struct _preview_update_msg *m;
 
@@ -1528,7 +1532,9 @@ save_message_infos_to_db (CamelFolderSummary *s, gboolean fresh_mirs, CamelExcep
 	}
 	CAMEL_SUMMARY_LOCK(s, summary_lock);
 	/* Push MessageInfo-es */
+	camel_db_begin_transaction(cdb, NULL);
 	g_hash_table_foreach (s->loaded_infos, save_to_db_cb, &args);
+	camel_db_end_transaction (cdb, NULL);
 	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
 /* FIXME[disk-summary] make sure we free the message infos that are loaded
  * are freed if not used anymore or should we leave that to the timer? */
@@ -1551,12 +1557,12 @@ camel_folder_summary_save_to_db (CamelFolderSummary *s, CamelException *ex)
 
 	d(printf ("\ncamel_folder_summary_save_to_db called \n"));
 	if (_PRIVATE(s)->need_preview && g_hash_table_size(_PRIVATE(s)->preview_updates)) {
-		camel_db_begin_transaction (s->folder->parent_store->cdb_w, NULL);
 		CAMEL_SUMMARY_LOCK(s, summary_lock);
+		camel_db_begin_transaction (s->folder->parent_store->cdb_w, NULL);
 		g_hash_table_foreach (_PRIVATE(s)->preview_updates, (GHFunc)msg_save_preview, s->folder);
 		g_hash_table_remove_all (_PRIVATE(s)->preview_updates);
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
 		camel_db_end_transaction (s->folder->parent_store->cdb_w, NULL);
+		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
 	}
 
 	if (!(s->flags & CAMEL_SUMMARY_DIRTY))
@@ -1570,35 +1576,29 @@ camel_folder_summary_save_to_db (CamelFolderSummary *s, CamelException *ex)
 
 	dd(printf("Saving %d/%d dirty records of %s\n", count, g_hash_table_size (s->loaded_infos), s->folder->full_name));
 
-	camel_db_begin_transaction (cdb, ex);
 
 	ret = save_message_infos_to_db (s, FALSE, ex);
 	if (ret != 0) {
-		camel_db_abort_transaction (cdb, ex);
 		/* Failed, so lets reset the flag */
 		s->flags |= CAMEL_SUMMARY_DIRTY;
 		return -1;
 	}
 
 	if (ex && camel_exception_is_set (ex) && strstr (camel_exception_get_description (ex), "26 columns but 28 values") != NULL) {
-		/* This is an error is previous migration. Let remigrate this folder alone. */
-		camel_db_abort_transaction (cdb, ex);
-		camel_db_reset_folder_version (cdb, s->folder->full_name, 0, ex);
 		g_warning ("Fixing up a broken summary migration on %s\n", s->folder->full_name);
 		/* Begin everything again. */
 		camel_db_begin_transaction (cdb, ex);
+		camel_db_reset_folder_version (cdb, s->folder->full_name, 0, ex);
+		camel_db_end_transaction (cdb, ex);
 
 		ret = save_message_infos_to_db (s, FALSE, ex);
 		if (ret != 0) {
-			camel_db_abort_transaction (cdb, ex);
 			/* Failed, so lets reset the flag */
 			s->flags |= CAMEL_SUMMARY_DIRTY;
 			return -1;
 		}
 	}
 
-	camel_db_end_transaction (cdb, ex);
-
 	record = (((CamelFolderSummaryClass *)(CAMEL_OBJECT_GET_CLASS(s)))->summary_header_to_db (s, ex));
 	if (!record) {
 		s->flags |= CAMEL_SUMMARY_DIRTY;
diff --git a/camel/camel-vee-folder.c b/camel/camel-vee-folder.c
index 89fa725..b34e728 100644
--- a/camel/camel-vee-folder.c
+++ b/camel/camel-vee-folder.c
@@ -83,6 +83,7 @@ static void vee_folder_remove_folder(CamelVeeFolder *vf, CamelFolder *source);
 static void folder_changed(CamelFolder *sub, CamelFolderChangeInfo *changes, CamelVeeFolder *vf);
 static void subfolder_deleted(CamelFolder *f, gpointer event_data, CamelVeeFolder *vf);
 static void folder_renamed(CamelFolder *f, const gchar *old, CamelVeeFolder *vf);
+static void vfolder_add_remove_transaction(CamelStore *parent_store, const gchar *full_name, GSList **uids, gboolean add, CamelException *ex);
 
 static CamelFolderClass *camel_vee_folder_parent;
 
@@ -1030,6 +1031,10 @@ struct _update_data {
 	CamelVeeFolder *folder_unmatched;
 	GHashTable *unmatched_uids;
 	gboolean rebuilt, correlating;
+
+	/* used for storing uids that needs to be updated in db later by unmatched_check_uid and
+   	   folder_added_uid */	   
+	GSList *uids_to_update;
 };
 
 static void
@@ -1048,12 +1053,10 @@ unmatched_check_uid(gchar *uidin, gpointer value, struct _update_data *u)
 	} else {
 		CamelVeeMessageInfo *mi = (CamelVeeMessageInfo *)camel_folder_summary_uid(((CamelFolder *)u->folder_unmatched)->summary, uid);
 		if (mi) {
-			CamelException ex = CAMEL_EXCEPTION_INITIALISER;
-			camel_db_delete_uid_from_vfolder_transaction (((CamelFolder *)u->folder_unmatched)->parent_store->cdb_w, ((CamelFolder *)u->folder_unmatched)->full_name, uid, &ex);
+			u->uids_to_update = g_slist_prepend (u->uids_to_update, (gpointer) camel_pstring_strdup (uid));
 			camel_folder_summary_remove_uid_fast (((CamelFolder *)u->folder_unmatched)->summary, uid);
 			camel_folder_change_info_remove_uid(u->folder_unmatched->changes, uid);
 			camel_message_info_free((CamelMessageInfo *)mi);
-			camel_exception_clear (&ex);
 		}
 	}
 }
@@ -1071,13 +1074,10 @@ folder_added_uid(gchar *uidin, gpointer value, struct _update_data *u)
 		/* FIXME[disk-summary] Handle exceptions */
 		/* FIXME[disk-summary] Make all these as transactions, just
 		 * testing atm */
-		if (u->rebuilt && !u->correlating) {
-			CamelException ex = CAMEL_EXCEPTION_INITIALISER;
-			camel_db_add_to_vfolder_transaction (((CamelFolder *) u->vf)->parent_store->cdb_w, ((CamelFolder *) u->vf)->full_name, (gchar *) camel_message_info_uid(mi), &ex);
-			camel_exception_clear (&ex);
-		}
+		if (u->rebuilt && !u->correlating) 
+			u->uids_to_update = g_slist_prepend (u->uids_to_update, (gpointer) camel_pstring_strdup (camel_message_info_uid (mi)));
 		if (!CAMEL_IS_VEE_FOLDER(u->source) && u->unmatched_uids != NULL) {
-			if (g_hash_table_lookup_extended(u->unmatched_uids, camel_message_info_uid(mi), (gpointer *)&oldkey, &oldval)) {
+			if (g_hash_table_lookup_extended(u->unmatched_uids, camel_message_info_uid(mi), (gpointer *)&oldkey, &oldval)){
 				n = GPOINTER_TO_INT (oldval);
 				g_hash_table_insert(u->unmatched_uids, oldkey, GINT_TO_POINTER(n+1));
 			} else {
@@ -1149,6 +1149,7 @@ vee_rebuild_folder(CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
 	u.unmatched_uids = unmatched_uids;
 	u.rebuilt = rebuilded;
 	u.correlating = correlating;
+	u.uids_to_update = NULL;
 
 	CAMEL_VEE_FOLDER_LOCK(vf, summary_lock);
 
@@ -1230,14 +1231,16 @@ vee_rebuild_folder(CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
 		camel_folder_summary_remove_range(folder->summary, start, last);
 
 	/* now matchhash contains any new uid's, add them, etc */
-	if (rebuilded && !correlating) {
-		camel_db_begin_transaction (folder->parent_store->cdb_w, NULL);
-
-	}
 	g_hash_table_foreach(matchhash, (GHFunc)folder_added_uid, &u);
 
-	if (rebuilded && !correlating)
+	if (rebuilded && !correlating && u.uids_to_update) {
+		const gchar *full_name;
+
+		/* FIXME[disk-summary] Handle exceptions */
+		camel_db_begin_transaction (folder->parent_store->cdb_w, NULL);
+		vfolder_add_remove_transaction (folder->parent_store, full_name, &(u.uids_to_update), TRUE, NULL);
 		camel_db_end_transaction (folder->parent_store->cdb_w, NULL);
+	}
 
 	if (folder_unmatched != NULL) {
 		/* scan unmatched, remove any that have vanished, etc */
@@ -1261,9 +1264,22 @@ vee_rebuild_folder(CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
 		}
 
 		/* now allhash contains all potentially new uid's for the unmatched folder, process */
-		if (!CAMEL_IS_VEE_FOLDER(source))
+		if (!CAMEL_IS_VEE_FOLDER(source)) {
 			g_hash_table_foreach(allhash, (GHFunc)unmatched_check_uid, &u);
 
+			if (u.uids_to_update) {
+				const gchar *full_name;
+
+				full_name = camel_folder_get_full_name (
+						CAMEL_FOLDER (u.folder_unmatched));
+				
+				camel_db_begin_transaction (CAMEL_FOLDER (u.folder_unmatched)->parent_store->cdb_w, NULL);
+				vfolder_add_remove_transaction (CAMEL_FOLDER (u.folder_unmatched)->parent_store, full_name, &(u.uids_to_update), FALSE, NULL);
+				camel_db_end_transaction (CAMEL_FOLDER (u.folder_unmatched)->parent_store->cdb_w, NULL);
+			}
+		
+		}
+
 		/* copy any changes so we can raise them outside the lock */
 		if (camel_folder_change_info_changed(folder_unmatched->changes)) {
 			unmatched_changes = folder_unmatched->changes;
@@ -1320,9 +1336,14 @@ vee_rebuild_folder(CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
 
 /* Hold all these with summary lock and unmatched summary lock held */
 static void
-folder_changed_add_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8], CamelVeeFolder *vf, gboolean use_db)
+folder_changed_add_uid (CamelFolder *sub,
+			const gchar *uid,
+			const gchar hash[8],
+			CamelVeeFolder *vf,
+			gboolean use_db,
+			GSList **m_added_l,
+			GSList **unm_added_l)
 {
-	CamelFolder *folder = (CamelFolder *)vf;
 	CamelVeeMessageInfo *vinfo;
 	const gchar *vuid;
 	gchar *oldkey;
@@ -1336,11 +1357,9 @@ folder_changed_add_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8],
 		return;
 
 	vuid = camel_message_info_uid(vinfo);
-	if (use_db) {
-		CamelException ex = CAMEL_EXCEPTION_INITIALISER;
-		camel_db_add_to_vfolder_transaction (folder->parent_store->cdb_w, folder->full_name, (gchar *)vuid, &ex);
-		camel_exception_clear (&ex);
-	}
+	if (use_db)
+		*m_added_l = g_slist_prepend (*m_added_l, (gpointer) camel_pstring_strdup (vuid));
+
 	camel_folder_change_info_add_uid(vf->changes,  vuid);
 	if ((vf->flags & CAMEL_STORE_FOLDER_PRIVATE) == 0 && !CAMEL_IS_VEE_FOLDER(sub) && folder_unmatched != NULL) {
 		if (g_hash_table_lookup_extended(unmatched_uids, vuid, (gpointer *)&oldkey, &oldval)) {
@@ -1353,7 +1372,7 @@ folder_changed_add_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8],
 		if (vinfo) {
 			CamelException ex = CAMEL_EXCEPTION_INITIALISER;
 			camel_folder_change_info_remove_uid(folder_unmatched->changes, vuid);
-			camel_db_delete_uid_from_vfolder_transaction (folder->parent_store->cdb_w, ((CamelFolder *)folder_unmatched)->full_name, vuid, &ex);
+			*unm_added_l = g_slist_prepend (*unm_added_l, (gpointer) camel_pstring_strdup (vuid));
 			camel_folder_summary_remove_uid_fast(((CamelFolder *)folder_unmatched)->summary, vuid);
 			camel_folder_free_message_info((CamelFolder *)folder_unmatched, (CamelMessageInfo *)vinfo);
 			camel_exception_clear (&ex);
@@ -1362,7 +1381,14 @@ folder_changed_add_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8],
 }
 
 static void
-folder_changed_remove_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8], gint keep, CamelVeeFolder *vf, gboolean use_db)
+folder_changed_remove_uid	(CamelFolder *sub,
+				const gchar *uid,
+				const gchar hash[8],
+				gint keep,
+				CamelVeeFolder *vf,
+				gboolean use_db,
+				GSList **m_removed_l,
+				GSList **unm_removed_l)
 {
 	CamelFolder *folder = (CamelFolder *)vf;
 	gchar *vuid, *oldkey;
@@ -1377,12 +1403,9 @@ folder_changed_remove_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8
 	strcpy(vuid+8, uid);
 
 	camel_folder_change_info_remove_uid(vf->changes, vuid);
-	if (use_db) {
-		/* FIXME[disk-summary] Handle exception */
-		CamelException ex = CAMEL_EXCEPTION_INITIALISER;
-		camel_db_delete_uid_from_vfolder_transaction (folder->parent_store->cdb_w, folder->full_name, vuid, &ex);
-		camel_exception_clear (&ex);
-	}
+	if (use_db)
+		*m_removed_l = g_slist_prepend (*m_removed_l, (gpointer) camel_pstring_strdup (vuid));
+
 	camel_folder_summary_remove_uid_fast(folder->summary, vuid);
 
 	if ((vf->flags & CAMEL_STORE_FOLDER_PRIVATE) == 0 && !CAMEL_IS_VEE_FOLDER(sub) && folder_unmatched != NULL) {
@@ -1411,7 +1434,7 @@ folder_changed_remove_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8
 			if (vinfo) {
 				CamelException ex = CAMEL_EXCEPTION_INITIALISER;
 				camel_folder_change_info_remove_uid(folder_unmatched->changes, vuid);
-				camel_db_delete_uid_from_vfolder_transaction (folder->parent_store->cdb_w, ((CamelFolder *)folder_unmatched)->full_name, vuid, &ex);
+				*unm_removed_l = g_slist_prepend (*unm_removed_l, (gpointer) camel_pstring_strdup (vuid));
 				camel_folder_summary_remove_uid_fast(((CamelFolder *)folder_unmatched)->summary, vuid);
 				camel_folder_free_message_info((CamelFolder *)folder_unmatched, (CamelMessageInfo *)vinfo);
 				camel_exception_clear (&ex);
@@ -1421,7 +1444,13 @@ folder_changed_remove_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8
 }
 
 static void
-folder_changed_change_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8], CamelVeeFolder *vf, gboolean use_db)
+folder_changed_change_uid	(CamelFolder *sub,
+				const gchar *uid,
+				const gchar hash[8],
+				CamelVeeFolder *vf,
+				gboolean use_db,
+				GSList **m_removed_l,
+				GSList **unm_removed_l)
 {
 	gchar *vuid;
 	CamelVeeMessageInfo *vinfo, *uinfo = NULL;
@@ -1452,7 +1481,7 @@ folder_changed_change_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8
 			camel_folder_free_message_info(sub, info);
 		} else {
 			if (vinfo) {
-				folder_changed_remove_uid(sub, uid, hash, FALSE, vf, use_db);
+				folder_changed_remove_uid (sub, uid, hash, FALSE, vf, use_db, m_removed_l, unm_removed_l);
 				camel_message_info_free((CamelMessageInfo *)vinfo);
 			}
 			if (uinfo)
@@ -1461,6 +1490,26 @@ folder_changed_change_uid(CamelFolder *sub, const gchar *uid, const gchar hash[8
 	}
 }
 
+static void
+vfolder_add_remove_transaction	(CamelStore *parent_store, const gchar *full_name, GSList **uids, gboolean add, CamelException *ex)
+{
+	GSList *l;
+
+	for (l = *uids; l != NULL; l = g_slist_next (l)) {
+		if (add)	
+			 camel_db_add_to_vfolder_transaction	(parent_store->cdb_w, (gchar *)full_name,
+				                                (gchar *) l->data, ex);
+		else
+			camel_db_delete_uid_from_vfolder_transaction 	
+								(parent_store->cdb_w, full_name,
+								 (gchar *) l->data, ex);
+	}
+	
+	g_slist_foreach (*uids, (GFunc) camel_pstring_free, NULL);
+	g_slist_free (*uids);
+	*uids = NULL;
+}
+
 struct _folder_changed_msg {
 	CamelSessionThreadMsg msg;
 	CamelFolderChangeInfo *changes;
@@ -1490,6 +1539,7 @@ folder_changed_change(CamelSession *session, CamelSessionThreadMsg *msg)
 	CamelVeeFolder *folder_unmatched = vf->parent_vee_store ? vf->parent_vee_store->folder_unmatched : NULL;
 	GHashTable *unmatched_uids = vf->parent_vee_store ? vf->parent_vee_store->unmatched_uids : NULL;
 	GPtrArray *present = NULL;
+	GSList *m_added_l = NULL, *m_removed_l = NULL, *unm_added_l = NULL, *unm_removed_l = NULL;
 
 	/* See vee_rebuild_folder. */
 	gboolean correlating = expression_is_correlating(vf->expression);
@@ -1558,16 +1608,13 @@ folder_changed_change(CamelSession *session, CamelSessionThreadMsg *msg)
 	if (folder_unmatched != NULL)
 		CAMEL_VEE_FOLDER_LOCK(folder_unmatched, summary_lock);
 
-	if (matches_changed || matches_added || changes->uid_removed->len||present)
-		camel_db_begin_transaction (folder->parent_store->cdb_w, NULL);
-
 	dd(printf("Vfolder '%s' subfolder changed '%s'\n", folder->full_name, sub->full_name));
 	dd(printf(" changed %u added %u removed %u\n", changes->uid_changed->len, changes->uid_added->len, changes->uid_removed->len));
 
 	/* Always remove removed uid's, in any case */
 	for (i=0;i<changes->uid_removed->len;i++) {
 		dd(printf("  removing uid '%s'\n", (gchar *)changes->uid_removed->pdata[i]));
-		folder_changed_remove_uid(sub, changes->uid_removed->pdata[i], hash, FALSE, vf, !correlating);
+		folder_changed_remove_uid (sub, changes->uid_removed->pdata[i], hash, FALSE, vf, !correlating, &m_removed_l, &unm_removed_l);
 	}
 
 	/* Add any newly matched or to unmatched folder if they dont */
@@ -1581,7 +1628,7 @@ folder_changed_change(CamelSession *session, CamelSessionThreadMsg *msg)
 			uid = changes->uid_added->pdata[i];
 			if (g_hash_table_lookup(matches_hash, uid)) {
 				dd(printf("  adding uid '%s' [newly matched]\n", (gchar *)uid));
-				folder_changed_add_uid(sub, uid, hash, vf, !correlating);
+				folder_changed_add_uid (sub, uid, hash, vf, !correlating, &m_added_l, &unm_added_l);
 			} else if ((vf->flags & CAMEL_STORE_FOLDER_PRIVATE) == 0) {
 				if (strlen(uid)+9 > vuidlen) {
 					vuidlen = strlen(uid)+64;
@@ -1614,12 +1661,12 @@ folder_changed_change(CamelSession *session, CamelSessionThreadMsg *msg)
 			 * vee_rebuild_folder will make any necessary removals
 			 * when it re-queries the entire source folder. */
 			for (i=0;i<always_changed->len;i++)
-				folder_changed_change_uid(sub, always_changed->pdata[i], hash, vf, !correlating);
+				folder_changed_change_uid (sub, always_changed->pdata[i], hash, vf, !correlating, &m_removed_l, &unm_removed_l);
 		} else {
 			GHashTable *ht_present = g_hash_table_new (g_str_hash, g_str_equal);
 
 			for (i=0;present && i<present->len;i++) {
-				folder_changed_change_uid(sub, present->pdata[i], hash, vf, !correlating);
+				folder_changed_change_uid (sub, present->pdata[i], hash, vf, !correlating, &m_removed_l, &unm_removed_l);
 				g_hash_table_insert (ht_present, present->pdata[i], present->pdata[i]);
 			}
 
@@ -1627,7 +1674,7 @@ folder_changed_change(CamelSession *session, CamelSessionThreadMsg *msg)
 				if (!present || !g_hash_table_lookup(ht_present, always_changed->pdata[i]))
 					/* XXX: IIUC, these messages haven't been deleted from the
 					 * source folder, so shouldn't "keep" be set to TRUE? */
-					folder_changed_remove_uid(sub, always_changed->pdata[i], hash, TRUE, vf, !correlating);
+					folder_changed_remove_uid (sub, always_changed->pdata[i], hash, TRUE, vf, !correlating, &m_removed_l, &unm_removed_l);
 			}
 
 			g_hash_table_destroy (ht_present);
@@ -1659,21 +1706,21 @@ folder_changed_change(CamelSession *session, CamelSessionThreadMsg *msg)
 				if (g_hash_table_lookup(matches_hash, uid)) {
 					/* A uid we dont have, but now it matches, add it */
 					dd(printf("  adding uid '%s' [newly matched]\n", uid));
-					folder_changed_add_uid(sub, uid, hash, vf, !correlating);
+					folder_changed_add_uid (sub, uid, hash, vf, !correlating, &m_added_l, &unm_added_l);
 				} else {
 					/* A uid we still don't have, just change it (for unmatched) */
-					folder_changed_change_uid(sub, uid, hash, vf, !correlating);
+					folder_changed_change_uid (sub, uid, hash, vf, !correlating, &m_removed_l, &unm_removed_l);
 				}
 			} else {
 				if ((vf->flags & CAMEL_STORE_VEE_FOLDER_AUTO) == 0
 				    || g_hash_table_lookup(matches_hash, uid)) {
 					/* still match, or we're not auto-updating, change event, (if it changed) */
 					dd(printf("  changing uid '%s' [still matches]\n", uid));
-					folder_changed_change_uid(sub, uid, hash, vf, !correlating);
+					folder_changed_change_uid (sub, uid, hash, vf, !correlating, &m_removed_l, &unm_removed_l);
 				} else {
 					/* No longer matches, remove it, but keep it in unmatched (potentially) */
 					dd(printf("  removing uid '%s' [did match]\n", uid));
-					folder_changed_remove_uid(sub, uid, hash, TRUE, vf, !correlating);
+					folder_changed_remove_uid (sub, uid, hash, TRUE, vf, !correlating, &m_removed_l, &unm_removed_l);
 				}
 				camel_message_info_free((CamelMessageInfo *)vinfo);
 			}
@@ -1682,7 +1729,7 @@ folder_changed_change(CamelSession *session, CamelSessionThreadMsg *msg)
 	} else {
 		/* stuff didn't match but it changed - check unmatched folder for changes */
 		for (i=0;i<changed->len;i++)
-			folder_changed_change_uid(sub, changed->pdata[i], hash, vf, !correlating);
+			folder_changed_change_uid (sub, changed->pdata[i], hash, vf, !correlating, &m_removed_l, &unm_removed_l);
 	}
 
 	if (folder_unmatched != NULL) {
@@ -1699,8 +1746,25 @@ folder_changed_change(CamelSession *session, CamelSessionThreadMsg *msg)
 		vf->changes = camel_folder_change_info_new();
 	}
 
-	if (matches_changed || matches_added || changes->uid_removed->len || present)
+	if (matches_changed || matches_added || changes->uid_removed->len || present) {
+		const gchar *full_name, *unm_full_name;
+
+		full_name = camel_folder_get_full_name (folder);
+		unm_full_name = camel_folder_get_full_name (CAMEL_FOLDER (folder_unmatched));
+		
+		camel_db_begin_transaction (folder->parent_store->cdb_w, NULL);
+
+		if (m_added_l)	
+			vfolder_add_remove_transaction (folder->parent_store, full_name, &m_added_l, TRUE, NULL);
+		if (m_removed_l)
+			vfolder_add_remove_transaction (folder->parent_store, full_name, &m_removed_l, FALSE, NULL);
+		if (unm_added_l)	
+			vfolder_add_remove_transaction (folder->parent_store, unm_full_name, &unm_added_l, TRUE, NULL);
+		if (unm_removed_l)
+			vfolder_add_remove_transaction (folder->parent_store, unm_full_name, &unm_removed_l, FALSE, NULL);
+
 		camel_db_end_transaction (folder->parent_store->cdb_w, NULL);
+	}
 	CAMEL_VEE_FOLDER_UNLOCK(vf, summary_lock);
 
 	/* Cleanup stuff on our folder */
@@ -2270,7 +2334,9 @@ camel_vee_folder_sync_headers (CamelFolder *vf, CamelException *ex)
 	/* Save the counts to DB */
 	start = time(NULL);
 	record = summary_header_to_db (vf->summary, ex);
+	camel_db_begin_transaction (vf->parent_store->cdb_w, NULL);
 	camel_db_write_folder_info_record (vf->parent_store->cdb_w, record, ex);
+	camel_db_end_transaction (vf->parent_store->cdb_w, NULL);
 	end = time(NULL);
 	dd(printf("Sync for vfolder '%s': %ld secs\n", vf->full_name, end-start));
 
@@ -2292,7 +2358,9 @@ camel_vee_folder_finalise (CamelObject *obj)
 	if (!vf->deleted) {
 		CamelException ex = CAMEL_EXCEPTION_INITIALISER;
 		record = summary_header_to_db (((CamelFolder *)vf)->summary, NULL);
+		camel_db_begin_transaction (((CamelFolder *) vf)->parent_store->cdb_w, NULL);
 		camel_db_write_folder_info_record (((CamelFolder *) vf)->parent_store->cdb_w, record, &ex);
+		camel_db_end_transaction (((CamelFolder *) vf)->parent_store->cdb_w, NULL);
 		g_free (record);
 		camel_exception_clear (&ex);
 	}
