diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index 47ab4eb..b328776 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -345,7 +345,8 @@ static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
 
 static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets)
 {
-	alpm_list_t *i, *j, *k;
+	alpm_list_t *i, *j;
+	alpm_list_t *groups = NULL;
 
 	if(targets) {
 		for(i = targets; i; i = alpm_list_next(i)) {
@@ -353,14 +354,7 @@ static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets)
 			for(j = syncs; j; j = alpm_list_next(j)) {
 				pmdb_t *db = alpm_list_getdata(j);
 				pmgrp_t *grp = alpm_db_readgrp(db, grpname);
-
-				if(grp) {
-					/* get names of packages in group */
-					for(k = alpm_grp_get_pkgs(grp); k; k = alpm_list_next(k)) {
-						printf("%s %s\n", grpname,
-								alpm_pkg_get_name(alpm_list_getdata(k)));
-					}
-				}
+				groups = alpm_list_add(groups, grp);
 			}
 		}
 	} else {
@@ -369,20 +363,25 @@ static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets)
 
 			for(j = alpm_db_getgrpcache(db); j; j = alpm_list_next(j)) {
 				pmgrp_t *grp = alpm_list_getdata(j);
-				const char *grpname = alpm_grp_get_name(grp);
+				groups = alpm_list_add(groups, grp);
+			}
+		}
+	}
 
-				if(level > 1) {
-					for(k = alpm_grp_get_pkgs(grp); k; k = alpm_list_next(k)) {
-						printf("%s %s\n", grpname,
-								alpm_pkg_get_name(alpm_list_getdata(k)));
-					}
-				} else {
-					/* print grp names only, no package names */
-					printf("%s\n", grpname);
-				}
+	for(i = groups; i; i = alpm_list_next(i)) {
+		pmgrp_t *grp = alpm_list_getdata(i);
+		const char *grpname = alpm_grp_get_name(grp);
+		if(level > 1) {
+			for(j = alpm_grp_get_pkgs(grp); j; j = alpm_list_next(j)) {
+				printf("%s %s\n", grpname,
+						alpm_pkg_get_name(alpm_list_getdata(j)));
 			}
+		} else {
+			/* print grp names only, no package names */
+			printf("%s\n", grpname);
 		}
 	}
+	alpm_list_free(groups);
 
 	return(0);
 }
