diff --git a/configure.ac b/configure.ac
index 2fa871e22f08065527904eaa5838ac3afe725220..a2539ff04ce06ef89cdac71bdd1e05423948e63e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -451,6 +451,26 @@ if test "$enable_opt" = "yes" ; then
    AX_CC_MAXOPT
    ac_test_CFLAGS="yes"
 
+   detected_ax_gcc_arch=`echo $ax_gcc_arch | awk '{print $1;}'`
+   
+   # Choose the best flags for the gravity sub-library on this compiler and architecture
+   if test "$ax_cv_c_compiler_vendor" = "intel"; then
+      if test "$detected_ax_gcc_arch" = "skylake-avx512"; then
+         GRAVITY_CFLAGS="-qopt-zmm-usage=high"
+      else
+         AC_MSG_WARN([No additional flags needed for gravity on this platform])
+      fi
+   elif test "$ax_cv_c_compiler_vendor" = "gnu"; then
+      if test "$detected_ax_gcc_arch" = "skylake-avx512"; then
+         GRAVITY_CFLAGS="-mprefer-vector-width=512"
+      else
+         AC_MSG_WARN([No additional flags needed for gravity on this platform])
+      fi
+   else
+      AC_MSG_WARN([Do not know what best gravity vectorization flags to choose for this compiler])
+   fi
+   AC_SUBST([GRAVITY_CFLAGS])
+
    # Check SSE & AVX support (some overlap with AX_CC_MAXOPT).
    # Don't use the SIMD_FLAGS result with Intel compilers. The -x<code>
    # value from AX_CC_MAXOPT should be sufficient.
diff --git a/src/Makefile.am b/src/Makefile.am
index c2a3a7755a188de6c907afe99e5ae1c4dfc5f3a8..408b19954b8beba0db35a413228b0ab34afb6e49 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,7 +19,7 @@
 AM_CFLAGS = $(HDF5_CPPFLAGS) $(GSL_INCS) $(FFTW_INCS) $(NUMA_INCS) $(GRACKLE_INCS)
 
 # Assign a "safe" version number
-AM_LDFLAGS = $(HDF5_LDFLAGS) $(FFTW_LIBS) -version-info 0:0:0
+AM_LDFLAGS = $(HDF5_LDFLAGS) $(FFTW_LIBS)
 
 # The git command, if available.
 GIT_CMD = @GIT_CMD@
@@ -31,11 +31,13 @@ EXTRA_LIBS = $(HDF5_LIBS) $(FFTW_LIBS) $(NUMA_LIBS) $(PROFILER_LIBS) $(TCMALLOC_
 MPI_LIBS = $(PARMETIS_LIBS) $(METIS_LIBS) $(MPI_THREAD_LIBS)
 MPI_FLAGS = -DWITH_MPI $(PARMETIS_INCS) $(METIS_INCS)
 
-# Build the libswiftsim library
+# Build the libswiftsim library and a convenience library just for the gravity tasks
 lib_LTLIBRARIES = libswiftsim.la
+noinst_LTLIBRARIES = libgrav.la 
 # Build a MPI-enabled version too?
 if HAVEMPI
 lib_LTLIBRARIES += libswiftsim_mpi.la
+noinst_LTLIBRARIES += libgrav_mpi.la
 endif
 
 # List required headers
@@ -70,7 +72,7 @@ EAGLE_FEEDBACK_SOURCES += feedback/EAGLE/feedback.c
 endif
 
 # Common source files
-AM_SOURCES = space.c runner_main.c runner_doiact_hydro.c runner_doiact_grav.c \
+AM_SOURCES = space.c runner_main.c runner_doiact_hydro.c \
     runner_doiact_stars.c runner_doiact_black_holes.c runner_ghost.c runner_recv.c \
     runner_sort.c runner_drift.c runner_black_holes.c runner_time_integration.c \
     runner_doiact_hydro_vec.c runner_others.c\
@@ -239,19 +241,28 @@ nobase_noinst_HEADERS = align.h approx_math.h atomic.h barrier.h cycle.h error.h
 		 pressure_floor/GEAR/pressure_floor_struct.h pressure_floor/none/pressure_floor_struct.h
 
 
+# Sources and special flags for the gravity library
+libgrav_la_SOURCES = runner_doiact_grav.c
+libgrav_la_CFLAGS = $(AM_CFLAGS) $(GRAVITY_CFLAGS) 
+libgrav_la_LDFLAGS = $(AM_LDFLAGS) $(EXTRA_LIBS)
+
+# Sources and special flags for the gravity MPI library
+libgrav_mpi_la_SOURCES = runner_doiact_grav.c
+libgrav_mpi_la_CFLAGS = $(AM_CFLAGS) $(GRAVITY_CFLAGS) -DWITH_MPI
+libgrav_mpi_la_LDFLAGS = $(AM_LDFLAGS) $(MPI_LIBS) $(EXTRA_LIBS)
+
 # Sources and flags for regular library
 libswiftsim_la_SOURCES = $(AM_SOURCES)
 libswiftsim_la_CFLAGS = $(AM_CFLAGS)
-libswiftsim_la_LDFLAGS = $(AM_LDFLAGS) $(EXTRA_LIBS)
-libswiftsim_la_LIBADD = $(GRACKLE_LIBS) $(VELOCIRAPTOR_LIBS)
+libswiftsim_la_LDFLAGS = $(AM_LDFLAGS) $(EXTRA_LIBS) -version-info 0:0:0
+libswiftsim_la_LIBADD = $(GRACKLE_LIBS) $(VELOCIRAPTOR_LIBS) libgrav.la
 
 # Sources and flags for MPI library
 libswiftsim_mpi_la_SOURCES = $(AM_SOURCES)
 libswiftsim_mpi_la_CFLAGS = $(AM_CFLAGS) $(MPI_FLAGS)
-libswiftsim_mpi_la_LDFLAGS = $(AM_LDFLAGS) $(MPI_LIBS) $(EXTRA_LIBS)
+libswiftsim_mpi_la_LDFLAGS = $(AM_LDFLAGS) $(MPI_LIBS) $(EXTRA_LIBS) -version-info 0:0:0
 libswiftsim_mpi_la_SHORTNAME = mpi
-libswiftsim_mpi_la_LIBADD = $(GRACKLE_LIBS) $(VELOCIRAPTOR_LIBS)
-
+libswiftsim_mpi_la_LIBADD = $(GRACKLE_LIBS) $(VELOCIRAPTOR_LIBS) $(MPI_LIBS) libgrav_mpi.la
 
 # Versioning. If any sources change then update the version_string.h file with
 # the current git revision and package version.