diff --git a/bfps/NavierStokes.py b/bfps/NavierStokes.py
index 694af6f4598bb234c9879d433720513b3f5dc1ae..458e5ed1e660d7018d2470a943fd77da29c57917 100644
--- a/bfps/NavierStokes.py
+++ b/bfps/NavierStokes.py
@@ -53,9 +53,10 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
                 hid_t Cdset, Cspace;
                 int ndims;
                 // store kspace information
-                Cdset = H5Dopen(parameter_file, "/kspace/kshell", H5P_DEFAULT);
+                Cdset = H5Dopen(stat_file, "/kspace/kshell", H5P_DEFAULT);
                 Cspace = H5Dget_space(Cdset);
                 H5Sget_simple_extent_dims(Cspace, dims, NULL);
+                H5Sclose(Cspace);
                 if (fs->nshells != dims[0])
                 {
                     std::cerr << "ERROR: computed nshells not equal to data file nshells\\n" << std::endl;
@@ -63,13 +64,13 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
                 }
                 H5Dwrite(Cdset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, fs->kshell);
                 H5Dclose(Cdset);
-                Cdset = H5Dopen(parameter_file, "/kspace/nshell", H5P_DEFAULT);
+                Cdset = H5Dopen(stat_file, "/kspace/nshell", H5P_DEFAULT);
                 H5Dwrite(Cdset, H5T_NATIVE_INT64, H5S_ALL, H5S_ALL, H5P_DEFAULT, fs->nshell);
                 H5Dclose(Cdset);
-                Cdset = H5Dopen(parameter_file, "/kspace/kM", H5P_DEFAULT);
+                Cdset = H5Dopen(stat_file, "/kspace/kM", H5P_DEFAULT);
                 H5Dwrite(Cdset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &fs->kM);
                 H5Dclose(Cdset);
-                Cdset = H5Dopen(parameter_file, "/kspace/dk", H5P_DEFAULT);
+                Cdset = H5Dopen(stat_file, "/kspace/dk", H5P_DEFAULT);
                 H5Dwrite(Cdset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &fs->dk);
                 H5Dclose(Cdset);
                 //endcpp
@@ -79,7 +80,7 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
                         '/statistics/moments/{0}'.format(field),
                         '/statistics/histograms/{0}'.format(field),
                         '/statistics/spectra/{0}_{0}'.format(field)]:
-                self.file_datasets_grow += ('Cdset = H5Dopen(parameter_file, "{0}", H5P_DEFAULT);\n'.format(key) +
+                self.file_datasets_grow += ('Cdset = H5Dopen(stat_file, "{0}", H5P_DEFAULT);\n'.format(key) +
                                             'Cspace = H5Dget_space(Cdset);\n' +
                                             'ndims = H5Sget_simple_extent_dims(Cspace, dims, NULL);\n' +
                                             'dims[0] += niter_todo/niter_stat;\n' +
@@ -154,14 +155,14 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
                        """]
         stat_template = """
                 //begincpp
-                        Cdset = H5Dopen(parameter_file, "{0}", H5P_DEFAULT);
+                        Cdset = H5Dopen(stat_file, "{0}", H5P_DEFAULT);
                         wspace = H5Dget_space(Cdset);
                         ndims = H5Sget_simple_extent_dims(wspace, dims, NULL);
                         mspace = H5Screate_simple(ndims, count, NULL);
                         H5Sselect_hyperslab(wspace, H5S_SELECT_SET, offset, NULL, count, NULL);
                         H5Dwrite(Cdset, {1}, mspace, wspace, H5P_DEFAULT, {2});
                         H5Dclose(Cdset);
-                        Cdset = H5Dopen(parameter_file, "{3}", H5P_DEFAULT);
+                        Cdset = H5Dopen(stat_file, "{3}", H5P_DEFAULT);
                         H5Dwrite(Cdset, {1}, mspace, wspace, H5P_DEFAULT, {4});
                         H5Sclose(mspace);
                         H5Sclose(wspace);
@@ -248,7 +249,7 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
                         temp_string = (std::string("/particles/") +
                                        std::string(ps{0}->name) +
                                        std::string("/{1}"));
-                        Cdset = H5Dopen(parameter_file, temp_string.c_str(), H5P_DEFAULT);
+                        Cdset = H5Dopen(stat_file, temp_string.c_str(), H5P_DEFAULT);
                         Cspace = H5Dget_space(Cdset);
                         ndims = H5Sget_simple_extent_dims(Cspace, dims, NULL);
                         dims[0] += niter_todo/niter_part;
@@ -288,16 +289,16 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
                                   std::string temp_string = (std::string("/particles/") +
                                                              std::string(ps{0}->name) +
                                                              std::string("/velocity"));
-                                  hid_t Cdset = H5Dopen(parameter_file, temp_string.c_str(), H5P_DEFAULT);
+                                  hid_t Cdset = H5Dopen(stat_file, temp_string.c_str(), H5P_DEFAULT);
                                   hid_t mspace, wspace;
                                   int ndims;
-                                  hsize_t count[3], offset[3], dims[3];
+                                  hsize_t count[3], offset[3];
+                                  wspace = H5Dget_space(Cdset);
+                                  ndims = H5Sget_simple_extent_dims(wspace, count, NULL);
                                   count[0] = 1;
                                   offset[0] = ps{0}->iteration / ps{0}->traj_skip;
                                   offset[1] = 0;
                                   offset[2] = 0;
-                                  wspace = H5Dget_space(Cdset);
-                                  ndims = H5Sget_simple_extent_dims(wspace, dims, NULL);
                                   mspace = H5Screate_simple(ndims, count, NULL);
                                   H5Sselect_hyperslab(wspace, H5S_SELECT_SET, offset, NULL, count, NULL);
                                   H5Dwrite(Cdset, H5T_NATIVE_DOUBLE, mspace, wspace, H5P_DEFAULT, ps{0}->rhs[0]);
@@ -307,7 +308,7 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
                                   temp_string = (std::string("/particles/") +
                                                  std::string(ps{0}->name) +
                                                  std::string("/acceleration"));
-                                  Cdset = H5Dopen(parameter_file, temp_string.c_str(), H5P_DEFAULT);
+                                  Cdset = H5Dopen(stat_file, temp_string.c_str(), H5P_DEFAULT);
                                   H5Dwrite(Cdset, H5T_NATIVE_DOUBLE, mspace, wspace, H5P_DEFAULT, acceleration);
                                   H5Sclose(mspace);
                                   H5Sclose(wspace);
@@ -327,13 +328,13 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
                                 'ps{1}->dt = dt;\n' +
                                 'ps{1}->iteration = iteration;\n' +
                                 update_field +
-                                'ps{1}->read(parameter_file);\n').format(self.C_dtype, self.particle_species)
+                                'ps{1}->read(stat_file);\n').format(self.C_dtype, self.particle_species)
         self.particle_loop += ((update_field +
                                'ps{0}->step();\n' +
                                 'if (ps{0}->iteration % niter_part == 0)\n' +
-                                'ps{0}->write(parameter_file, false);\n').format(self.particle_species) +
+                                'ps{0}->write(stat_file, false);\n').format(self.particle_species) +
                                output_vel_acc)
-        self.particle_end += ('ps{0}->write(parameter_file);\n' +
+        self.particle_end += ('ps{0}->write(stat_file);\n' +
                               'delete ps{0};\n').format(self.particle_species)
         self.particle_species += 1
         return None
diff --git a/bfps/code.py b/bfps/code.py
index 6cb557f93701533ac235fd64bc7f5c13b8ed13db..478ef8ad9b61c6f3e1d76bdb2c650f9060787103 100644
--- a/bfps/code.py
+++ b/bfps/code.py
@@ -61,7 +61,7 @@ class code(base):
         self.variables = 'int myrank, nprocs;\n'
         self.variables += 'int iteration;\n'
         self.variables += 'char simname[256], fname[256];\n'
-        self.variables += ('hid_t parameter_file, Cdset;\n')
+        self.variables += ('hid_t parameter_file, stat_file, Cdset;\n')
         self.definitions = ''
         self.main_start = """
                 //begincpp
@@ -91,7 +91,7 @@ class code(base):
                     read_parameters(parameter_file);
                     H5Fclose(parameter_file);
                     if (myrank == 0)
-                        parameter_file = H5Fopen(fname, H5F_ACC_RDWR, H5P_DEFAULT);
+                        stat_file = H5Fopen(fname, H5F_ACC_RDWR, H5P_DEFAULT);
                 //endcpp
                 """
         for ostream in ['cout', 'cerr']:
@@ -101,10 +101,10 @@ class code(base):
                     // clean up
                     if (myrank == 0)
                     {
-                        Cdset = H5Dopen(parameter_file, "iteration", H5P_DEFAULT);
+                        Cdset = H5Dopen(stat_file, "iteration", H5P_DEFAULT);
                         H5Dwrite(Cdset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &iteration);
                         H5Dclose(Cdset);
-                        H5Fclose(parameter_file);
+                        H5Fclose(stat_file);
                     }
                     fftwf_mpi_cleanup();
                     fftw_mpi_cleanup();