diff --git a/bin/dunecontrol b/bin/dunecontrol
index 2ef938bf124ad636c48c011020f20a88a4b505be..bf233d1012c3d7afd1da56d156618da6eba1eb12 100755
--- a/bin/dunecontrol
+++ b/bin/dunecontrol
@@ -84,7 +84,7 @@ build_modules() {
       cd "$path"
       eval_control $runcommand $path/$CONTROL
     ); then echo "--- Failed to build $module ---"; exit 1; fi
-    echo "--- $module done ---"
+    eval echo "--- \$NAME_${module} done ---"
   done
 }
 
@@ -311,10 +311,10 @@ usage () {
 # create the module list
 create_module_list() {
   find_modules_in_path
-  if test "x$MODULE" = "x"; then
-	sort_modules $MODULES
-  else
+  if test "x$SEARCH_MODULES" != "x"; then
 	sort_modules $SEARCH_MODULES
+  else
+	sort_modules $MODULES
   fi
   if test "x$ONLY" != x; then
     export MODULES="$ONLY"
@@ -345,6 +345,11 @@ for i in $COMMANDS; do
   export ${COMMAND}_FLAGS=""
 done
 
+# clear variables
+export SEARCH_MODULES=""
+export MODULES=""
+export ONLY=""
+
 # parse commandline parameters
 while test $# -gt 0; do
     # get option
diff --git a/bin/dunemodules.inc b/bin/dunemodules.inc
index 42d857b4e07a09129326ee6cf27c6d3f632e10b0..2d484885c40d7efbe53e5350853fabebf828d9a4 100644
--- a/bin/dunemodules.inc
+++ b/bin/dunemodules.inc
@@ -124,6 +124,9 @@ EOF
 # sort $MODULES according to the dependencies
 #
 sort_modules() {
+  # reset list
+  export SORTEDMODULES=""
+  # handle each modules passed as parameter
   for m in "$@"; do
     # did we find a module file for this mopdule?
     if test "x$(eval echo \$HAVE_$m)" != "x"; then
@@ -133,7 +136,12 @@ sort_modules() {
       exit 1
     fi
   done
+  # save result
   export MODULES="$SORTEDMODULES"
+  # clean up temporary variables
+  for m in $MODULES; do
+    export SORT_DONE_${m}=""
+  done
 }
 
 #
@@ -150,7 +158,7 @@ _sort_module() {
     echo "ERROR: invalid module name $module" > /dev/stderr
     exit 1
   fi
-  if test "x$(eval echo \$SORT_DONE_${command}_${module})" != "xyes"; then
+  if test "x$(eval echo \$SORT_DONE_${module})" != "xyes"; then
     # resolve dependencies
     for name in $(eval "echo \$DEPS_$module"); do
 	  dep=$(fix_variable_name $name)
@@ -184,7 +192,7 @@ _sort_module() {
       fi
     done
     # insert this module into the list
-    export SORT_DONE_${command}_${module}=yes
+    export SORT_DONE_${module}=yes
     export SORTEDMODULES="$SORTEDMODULES $module"
   fi
 }