diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c
index 81c287c..c182d36 100644
--- a/lib/libalpm/sync.c
+++ b/lib/libalpm/sync.c
@@ -414,7 +414,8 @@ int _alpm_sync_prepare(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync
 		for(i = list; i; i = i->next) {
 			/* add the dependencies found by resolvedeps to the transaction set */
 			pmpkg_t *spkg = i->data;
-			if(!_alpm_sync_find(trans->packages, alpm_pkg_get_name(spkg))) {
+			pmsyncpkg_t *tmpsync = _alpm_sync_find(trans->packages, alpm_pkg_get_name(spkg));
+			if(!tmpsync) {
 				pmsyncpkg_t *sync = _alpm_sync_new(PM_SYNC_TYPE_DEPEND, spkg, NULL);
 				if(sync == NULL) {
 					ret = -1;
@@ -427,18 +428,26 @@ int _alpm_sync_prepare(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync
 				/* remove the original targets from the list if requested */
 				if((trans->flags & PM_TRANS_FLAG_DEPENDSONLY)) {
 					void *vpkg;
-					pmsyncpkg_t *sync;
 
 					_alpm_log(PM_LOG_DEBUG, "removing package %s-%s from the transaction targets",
 										alpm_pkg_get_name(spkg), alpm_pkg_get_version(spkg));
 
-					sync = _alpm_sync_find(trans->packages, alpm_pkg_get_name(spkg));
-					trans->packages = alpm_list_remove(trans->packages, sync, syncpkg_cmp, &vpkg);
+					trans->packages = alpm_list_remove(trans->packages, tmpsync, syncpkg_cmp, &vpkg);
 					_alpm_sync_free(vpkg);
 				}
 			}
 		}
 
+		/* if we removed some targets, we must resolve dependencies again */
+		/* since PM_TRANS_FLAG_DEPENDSONLY is used nowhere else, we can do
+		 * a recursive call, type of trans->packages is PM_SYNC_TYPE_DEPEND*/
+		if((trans->flags & PM_TRANS_FLAG_DEPENDSONLY)) {
+			trans->flags ^= PM_TRANS_FLAG_DEPENDSONLY;
+			alpm_list_free(list);
+			EVENT(trans, PM_TRANS_EVT_RESOLVEDEPS_DONE, NULL, NULL);			
+			return (_alpm_sync_prepare(trans, db_local, dbs_sync, data));
+		}	
+
 		/* re-order w.r.t. dependencies */
 		alpm_list_t *sortlist = NULL;
 		alpm_list_t *newpkgs = NULL;
