diff --git a/cpp/kspace.cpp b/cpp/kspace.cpp
index c2d941bd590025e0b64ee78d936a293fca7938e0..cdb32d5dc19b12bc4a0d79c1d3c5a0b2128728e1 100644
--- a/cpp/kspace.cpp
+++ b/cpp/kspace.cpp
@@ -148,8 +148,11 @@ kspace<be, dt>::kspace(
             this->nshells,
             MPI_DOUBLE, MPI_SUM, this->layout->comm);
     for (int n=0; n<this->nshells; n++){
-        volatile double nnshell = std::max(double(this->nshell[n]), 1.0);
-	    this->kshell[n] /= nnshell;
+        // using the `volatile` line instead of the if gets the code to work
+        //volatile double nnshell = std::max(double(this->nshell[n]), 1.0);
+        if (this->nshell[n] > 0)
+	        //this->kshell[n] /= nnshell;
+	        this->kshell[n] /= this->nshell[n];
     }
 }