diff --git a/bin/duneproject b/bin/duneproject
index 1ccdf140a3b38bec4bc96adee0ed367fa1c9b528..9bb37461c71281029f69b765e31a8d3bb8670c4d 100755
--- a/bin/duneproject
+++ b/bin/duneproject
@@ -496,8 +496,31 @@ noinst_PROGRAMS = ${CMODULE}
 
 ${CMODULE}_SOURCES = $CMODULE.cc
 
-${CMODULE}_CXXFLAGS = \$(MPI_CPPFLAGS) \$(UG_CPPFLAGS) \$(AMIRAMESH_CPPFLAGS) \$(ALBERTA_CPPFLAGS) \$(ALUGRID_CPPFLAGS)
-${CMODULE}_LDADD = \$(MPI_LDFLAGS) \$(ALBERTA_LDFLAGS) \$(ALBERTA_LIBS) \$(ALUGRID_LDFLAGS) \$(ALUGRID_LIBS) \$(AMIRAMESH_LDFLAGS) \$(AMIRAMESH_LIBS) \$(UG_LDFLAGS) \$(UG_LIBS) \$(MPI_LDFLAGS) \$(DUNE_LDFLAGS) \$(DUNE_LIBS)
+${CMODULE}_CXXFLAGS = \$(AM_CXXFLAGS) \\
+	\$(DUNEMPICPPFLAGS) \\
+	\$(UG_CPPFLAGS) \\
+	\$(AMIRAMESH_CPPFLAGS) \\
+	\$(ALBERTA_CPPFLAGS) \\
+	\$(ALUGRID_CPPFLAGS)
+# The libraries have to be given in reverse order (most basic libraries
+# last).  Also, due to some misunderstanding, a lot of libraries include the
+# -L option in LDFLAGS instead of LIBS -- so we have to include the LDFLAGS
+# here as well.
+${CMODULE}_LDADD = \\
+	\$(DUNE_LDFLAGS) \$(DUNE_LIBS) \\
+	\$(ALUGRID_LDFLAGS) \$(ALUGRID_LIBS) \\
+	\$(ALBERTA_LDFLAGS) \$(ALBERTA_LIBS) \\
+	\$(AMIRAMESH_LDFLAGS) \$(AMIRAMESH_LIBS) \\
+	\$(UG_LDFLAGS) \$(UG_LIBS) \\
+	\$(DUNEMPILIBS)	\\
+	\$(LDADD)
+${CMODULE}_LDFLAGS = \$(AM_LDFLAGS) \\
+	\$(DUNEMPILDFLAGS) \\
+	\$(UG_LDFLAGS) \\
+	\$(AMIRAMESH_LDFLAGS) \\
+	\$(ALBERTA_LDFLAGS) \\
+	\$(ALUGRID_LDFLAGS) \\
+	\$(DUNE_LDFLAGS)
 
 # don't follow the full GNU-standard
 # we need automake 1.5
diff --git a/dune/common/test/Makefile.am b/dune/common/test/Makefile.am
index 0f6a09f8c412173a5f53b010bdb8c49d11bcd7e1..c13e00d246563af0575ab95d82225d7ae61a8474 100644
--- a/dune/common/test/Makefile.am
+++ b/dune/common/test/Makefile.am
@@ -39,7 +39,7 @@ check_PROGRAMS = $(TESTPROGS)
 
 noinst_HEADERS = dummyiterator.hh
 
-AM_LDFLAGS = $(LOCAL_LIBS)
+LDADD = $(LOCAL_LIBS)
 
 # define the programs
 smallobject_SOURCES = smallobject.cc
@@ -48,7 +48,7 @@ bitsetvectortest_SOURCES = bitsetvectortest.cc
 
 nullptr_test_SOURCES = nullptr-test.cc nullptr-test2.cc
 nullptr_test_fail_SOURCES = nullptr-test.cc
-nullptr_test_fail_CPPFLAGS = -DFAIL
+nullptr_test_fail_CPPFLAGS = $(AM_CPPFLAGS) -DFAIL
 
 static_assert_test_SOURCES = static_assert_test.cc
 static_assert_test_fail_SOURCES = static_assert_test_fail.cc
@@ -90,18 +90,18 @@ settest_SOURCES=settest.cc
 gcdlcmtest_SOURCES = gcdlcmtest.cc
 
 mpihelpertest_SOURCES = mpihelpertest.cc
-mpihelpertest_CXXFLAGS = $(MPI_CPPFLAGS)
-mpihelpertest_LDFLAGS = $(LOCAL_LIBS) $(MPI_LDFLAGS) $(MPI_LIBS)
+mpihelpertest_CPPFLAGS = $(AM_CPPFLAGS) $(DUNEMPICPPFLAGS)
+mpihelpertest_LDADD = $(DUNEMPILIBS) $(LDADD)
+mpihelpertest_LDFLAGS = $(AM_LDFLAGS) $(DUNEMPILDFLAGS)
 
 mpicollcomm_SOURCES = mpicollectivecommunication.cc
-mpicollcomm_CXXFLAGS = $(MPI_CPPFLAGS)
-mpicollcomm_LDFLAGS = $(LOCAL_LIBS) $(MPI_LDFLAGS) $(MPI_LIBS)
+mpicollcomm_CPPFLAGS = $(AM_CPPFLAGS) $(DUNEMPICPPFLAGS)
+mpicollcomm_LDADD = $(DUNEMPILIBS) $(LDADD)
+mpicollcomm_LDFLAGS = $(AM_LDFLAGS) $(DUNEMPILDFLAGS)
 
 singletontest_SOURCES = singletontest.cc
-singletontest_LDFLAGS = $(LOCAL_LIBS)
 
 utilitytest_SOURCES = utilitytest.cc
-utilitytest_LDFLAGS = $(LOCAL_LIBS)
 
 testfassign1_SOURCES = testfassign.cc testfassign2.cc
 testfassign1_CPPFLAGS = $(AM_CPPFLAGS) -D_N=3 -D_VALUES="1,2,3"
diff --git a/m4/Makefile.am b/m4/Makefile.am
index 05a12cada61aa3da297f496ca18def2d91404c9a..577b171abb4e0413751311f2ea180fec85fe605d 100644
--- a/m4/Makefile.am
+++ b/m4/Makefile.am
@@ -2,9 +2,12 @@
 
 ALLM4S = acx_blas.m4 acx_lapack.m4 acx_mpi.m4 acx_pthread.m4 \
          ax_check_gl.m4 ax_lang_compiler_ms.m4 dune_all.m4 \
-         dune_autobuild.m4 dune_common.m4 dune_compiler.m4	\
-	 dune_deprecated.m4					\
-	 dune_deprecated_cppflags.m4				\
+         dune_autobuild.m4			\
+	 dune_check_lib.m4			\
+	 dune_common.m4				\
+	 dune_compiler.m4			\
+	 dune_deprecated.m4			\
+	 dune_deprecated_cppflags.m4		\
          dune_docu.m4 dune_exprtmpl.m4  dune_linkcxx.m4 \
 	 dune.m4 \
          dune_mpi.m4 dune_streams.m4 \
diff --git a/m4/dune_check_lib.m4 b/m4/dune_check_lib.m4
new file mode 100644
index 0000000000000000000000000000000000000000..f3b350e9c5a539681495694b776af5466d78c4ef
--- /dev/null
+++ b/m4/dune_check_lib.m4
@@ -0,0 +1,20 @@
+dnl -*- autoconf -*-
+# Just like AC_CHECK_LIB, but in allow a library path to be specified.  The
+# default ACTION-IF-FOUND is extended to also add that library path to LIBS.
+#
+# DUNE_CHECK_LIB_EXT(PATH, LIBRARY, FUNCTION, [ACTION-IF-FOUND],
+#                    [ACTION-IF-NOT-FOUND], [OTHER-LIBRARIES])
+AC_DEFUN([DUNE_CHECK_LIB_EXT],
+[
+  dune_cle_save_LDFLAGS="$LDFLAGS"
+  LDFLAGS="$LDFLAGS -L$1"
+  m4_ifelse(m4_eval([$# <= 3]), [1],
+    [AC_CHECK_LIBS([$2], [$3],
+      [
+        LIBS="-L$1 -l$2 $LIBS"
+        AC_DEFINE([HAVE_LIB]m4_translit([[$2]], [-a-z], [_A-Z]), [1],
+                  [Define to 1 if you have the `$2' library (-l$2).])
+      ])],
+    [AC_CHECK_LIBS(m4_shift($@))])
+  LDFLAGS="$dune_cle_save_LDFLAGS"
+])
diff --git a/m4/dune_mpi.m4 b/m4/dune_mpi.m4
index fd92dfef182ead0bea1b87d8d10f77c1e5d90409..473b2f046ac902deb3487728f45cd68926899e3f 100644
--- a/m4/dune_mpi.m4
+++ b/m4/dune_mpi.m4
@@ -51,6 +51,39 @@ dnl -*- autoconf -*-
 #   --disable-parallel
 # [Christian 9.7.2006]
 
+# DUNE_MPI()
+#
+# With the help of ACX_MPI find the MPI compiler, and with the help of
+# MPI_CONFIG find the libraries and compiler and linker options necessary to
+# compile MPI programs with the standard compiler.  Will set the following
+# things:
+#
+# configure shell variables:
+#   MPICC
+#   MPILIBS
+#   DUNEMPICPPFLAGS
+#   DUNEMPILDFLAGS
+#   DUNEMPILIBS
+#   MPI_CPPFLAGS (deprecated in favor of DUNEMPICPPFLAGS)
+#   MPI_LDFLAGS (deprecated in favor of DUNEMPILIBS and DUNEMPILDFLAGS)
+#   dune_MPI_VERSION
+#   with_mpi
+#
+# configure substitutions/Makefile variables
+#   MPICC
+#   MPILIBS
+#   DUNEMPICPPFLAGS
+#   DUNEMPILDFLAGS
+#   DUNEMPILIBS
+#   MPI_CPPFLAGS (deprecated in favor of DUNEMPICPPFLAGS)
+#   MPI_LDFLAGS (deprecated in favor of DUNEMPILIBS and DUNEMPILDFLAGS)
+#   MPI_VERSION (set from dune_MPI_VERSION)
+#
+# preprocessor defines
+#   HAVE_MPI (defined when $with_mpi!=no)
+#
+# automake conditionals
+#   MPI (true when $with_mpi!=no)
 AC_DEFUN([DUNE_MPI],[
   AC_PREREQ(2.50) dnl for AC_LANG_CASE
 
diff --git a/m4/hdf5.m4 b/m4/hdf5.m4
index caacb860a66c7c2ada7b336df4b32e09143fbde2..b77409da5cbfcfc18a6130e065c1480854767f84 100644
--- a/m4/hdf5.m4
+++ b/m4/hdf5.m4
@@ -39,9 +39,9 @@ AC_DEFUN([DUNE_PATH_HDF5],[
 
  # test if we are parallel
  AC_CHECK_DECL(H5_HAVE_PARALLEL, [dnl
-	LIBS="$LIBS $MPI_LIB"
-	LDFLAGS="$LDFLAGS $MPI_LDFLAGS"
-	CPPFLAGS="$CPPFLAGS $MPI_CPPFLAGS"
+	LIBS="$DUNEMPILIBS $LIBS"
+	LDFLAGS="$LDFLAGS $DUNEMPILDFLAGS"
+	CPPFLAGS="$CPPFLAGS $DUNEMPICPPFLAGS"
 	HDF5_PARALLEL=1],[],[#include"H5pubconf.h"])
 
  # test for an arbitrary header
diff --git a/m4/parmetis.m4 b/m4/parmetis.m4
index 088e2456bca5c1c72105e2427d9cda9d18d93db1..e3db04721887ac30f991c99fd2b3323739096130 100644
--- a/m4/parmetis.m4
+++ b/m4/parmetis.m4
@@ -1,4 +1,5 @@
-#! /bin/bash
+dnl -*- mode: autoconf; tab-width: 8; indent-tabs-mode: nil; -*-
+dnl vi: set et ts=8 sw=2 sts=2:
 # $Id$
 # searches for ParMetis headers and libs
 
@@ -55,13 +56,14 @@ AC_DEFUN([DUNE_PATH_PARMETIS],[
   ac_save_LIBS="$LIBS"
   
   ## do nothing if --without-parmetis is used
-  if test x"$MPI_LDFLAGS" != x"" && test x"$with_parmetis" != x"no" ; then
+  if test x"$with_mpi" != x"no" && test x"$with_parmetis" != x"no" ; then
           
       # defaultpath
       PARMETIS_LIB_PATH="$with_parmetis$lib_path"
       PARMETIS_INCLUDE_PATH="$with_parmetis$lib_path"
                   
-      PARMETIS_LIBS="-L$PARMETIS_LIB_PATH $MPI_LDFLAGS $MPI_LIBS"
+      PARMETIS_LIBS="-L$PARMETIS_LIB_PATH -lmetis $DUNEMPILIBS -lm"
+      PARMETIS_LDFLAGS="$DUNEMPILDFLAGS"
 
       # set variables so that tests can use them
       CPPFLAGS="$CPPFLAGS -I$PARMETIS_INCLUDE_PATH $MPI_CPPFLAGS"
@@ -80,22 +82,27 @@ AC_DEFUN([DUNE_PATH_PARMETIS],[
       
       # if header is found check for the libs
 
-      LIBS="$LIBS $PARMETIS_LIBS -lm"
+      LIBS="$DUNEMPILIBS -lm $LIBS"
       
       if test x$HAVE_PARMETIS = x1 ; then
-	  AC_CHECK_LIB(metis, [metis_partgraphkway],[
-		  PARMETIS_LIBS="$PARMETIS_LIBS -lmetis"
-		  LIBS="$LIBS -lmetis"],[
+	  DUNE_CHECK_LIB_EXT(["$PARMETIS_LIB_PATH"], [metis], [metis_partgraphkway],
+              [
+		  PARMETIS_LIBS="-L$PARMETIS_LIB_PATH -lmetis $DUNEMPILIBS -lm"
+		  LIBS="$PARMETIS_LIBS $ac_save_LIBS"
+              ],[
 		  HAVE_PARMETIS="0"
-		  AC_MSG_WARN(libmetis not found!)])
+		  AC_MSG_WARN(libmetis not found!)
+              ])
       fi
 
       if test x$HAVE_PARMETIS = x1 ; then
-	  AC_CHECK_LIB(parmetis, [parmetis_v3_partkway],[
-		  PARMETIS_LIBS="$PARMETIS_LIBS -lparmetis -lmetis"
-		  HAVE_PARMETIS="1"],[
+	  DUNE_CHECK_LIB_EXT(["$PARMETIS_LIB_PATH"], [parmetis], [parmetis_v3_partkway],
+              [
+		  PARMETIS_LIBS="-L$PARMETIS_LIB_PATH -lparmetis -lmetis $DUNEMPILIBS -lm"
+              ],[
 		  HAVE_PARMETIS="0"
-		  AC_MSG_WARN(libparmetis not found!)])
+		  AC_MSG_WARN(libparmetis not found!)
+              ])
       fi
 
 #      AC_LANG_POP([C++])
@@ -107,6 +114,7 @@ AC_DEFUN([DUNE_PATH_PARMETIS],[
       AC_MSG_CHECKING(ParMETIS in $with_parmetis)
       if test x$HAVE_PARMETIS = x1 ; then
 	  AC_SUBST(PARMETIS_LIBS, $PARMETIS_LIBS)
+	  AC_SUBST(PARMETIS_LDFLAGS, $PARMETIS_LDFLAGS)
 	  AC_SUBST(PARMETIS_CPPFLAGS, $PARMETIS_CPPFLAGS)
 	  AC_DEFINE(HAVE_PARMETIS,ENABLE_PARMETIS,[Define if you have the Parmetis library.
 		  This is only true if MPI was found by configure 
@@ -114,7 +122,8 @@ AC_DEFUN([DUNE_PATH_PARMETIS],[
 	  AC_MSG_RESULT(ok)
 	  
     # add to global list
-	  DUNE_PKG_LIBS="$DUNE_PKG_LIBS $PARMETIS_LIBS"
+	  DUNE_PKG_LIBS="$PARMETIS_LIBS $DUNE_PKG_LIBS"
+	  DUNE_PKG_LDFLAGS="$DUNE_PKG_LDFLAGS $PARMETIS_LDFLAGS"
 	  DUNE_PKG_CPPFLAGS="$DUNE_PKG_CPPFLAGS $PARMETIS_CPPFLAGS"
 	  
     # re-set variable correctly
@@ -140,7 +149,3 @@ AC_DEFUN([DUNE_PATH_PARMETIS],[
   DUNE_ADD_SUMMARY_ENTRY([ParMETIS],[$with_parmetis])
 
 ])
-
-dnl Local Variables:
-dnl mode: shell-script
-dnl End:
diff --git a/m4/superlu-dist.m4 b/m4/superlu-dist.m4
index 1facc650eb34c57ef2fd77826a3c975b03018769..d1789e2c52b55af8328251694246255aab6b32f6 100644
--- a/m4/superlu-dist.m4
+++ b/m4/superlu-dist.m4
@@ -95,10 +95,10 @@ AC_DEFUN([DUNE_PATH_SUPERLU_DIST],[
 	    SUPERLU_DIST_LIB_PATH="$with_superlu_dist/$my_lib_path"
 	    SUPERLU_DIST_INCLUDE_PATH="$with_superlu_dist/$my_include_path"
 	    
-	    SUPERLU_DIST_LDFLAGS="-L$SUPERLU_DIST_LIB_PATH $MPI_LDFLAGS"
+	    SUPERLU_DIST_LDFLAGS="-L$SUPERLU_DIST_LIB_PATH $DUNEMPILDFLAGS"
 	    
       # set variables so that tests can use them
-	    CPPFLAGS="$CPPFLAGS -I$SUPERLU_DIST_INCLUDE_PATH $MPI_CPPFLAGS"
+	    CPPFLAGS="$CPPFLAGS -I$SUPERLU_DIST_INCLUDE_PATH $DUNEMPICPPFLAGS"
 	    
       # check for central header
 	    AC_CHECK_HEADER([superlu_ddefs.h],[
@@ -108,7 +108,7 @@ AC_DEFUN([DUNE_PATH_SUPERLU_DIST],[
 		    AC_MSG_WARN([superlu_ddefs.h not found in $SUPERLU_DIST_INCLUDE_PATH with $CPPFLAGS])]
 	    )
 
-	    SUPERLU_DIST_CPPFLAGS="-I$SUPERLU_DIST_INCLUDE_PATH $MPI_CPPFLAGS"
+	    SUPERLU_DIST_CPPFLAGS="-I$SUPERLU_DIST_INCLUDE_PATH $DUNEMPICPPFLAGS"
 	    
       # if header is found check for the libs
 	    	    
@@ -116,8 +116,8 @@ AC_DEFUN([DUNE_PATH_SUPERLU_DIST],[
 
 		# set variables so that tests can use them
 		OLDFLAGS="$LDFLAGS"
-		LDFLAGS="$LDFLAGS -L$SUPERLU_DIST_LIB_PATH $MPI_LDFLAGS"
-		LIBS="$BLAS_LIBS $LIBS $FLIBS $MPILIBS $MPI_LDFLAGS"
+		LDFLAGS="$LDFLAGS -L$SUPERLU_DIST_LIB_PATH $DUNEMPILDFLAGS"
+		LIBS="$BLAS_LIBS $LIBS $FLIBS $DUNEMPILIBS"
 
 		AC_CHECK_LIB(superlu-mpi, [pdgssvx],
 		    [dnl