diff --git a/ChemistryLib/PhreeqcIO.cpp b/ChemistryLib/PhreeqcIO.cpp
index 8149ec0a32e31890e2071ecc6a422731cf61ce64..3094cc50ec159616c328b4742801996742a5f7b8 100644
--- a/ChemistryLib/PhreeqcIO.cpp
+++ b/ChemistryLib/PhreeqcIO.cpp
@@ -531,33 +531,33 @@ std::vector<std::string> const PhreeqcIO::getComponentList() const
     return component_names;
 }
 
+template <typename Reactant>
+static double averageReactantAmount(
+    Reactant const& reactant,
+    std::vector<GlobalIndexType> const& chemical_system_indices)
+{
+    double const sum = std::accumulate(
+        chemical_system_indices.begin(), chemical_system_indices.end(), 0.0,
+        [&](double const s, GlobalIndexType const id) {
+            return s + (*reactant.amount)[id];
+        });
+    return sum / chemical_system_indices.size();
+}
+
 void PhreeqcIO::computeSecondaryVariable(
     std::size_t const ele_id,
     std::vector<GlobalIndexType> const& chemical_system_indices)
 {
-    auto const num_chemical_systems = chemical_system_indices.size();
     for (auto& kinetic_reactant : _chemical_system->kinetic_reactants)
     {
-        double amount_avg = std::accumulate(
-            chemical_system_indices.begin(), chemical_system_indices.end(), 0.0,
-            [&](double amount, auto const& id) {
-                return amount + (*kinetic_reactant.amount)[id];
-            });
-
-        amount_avg /= num_chemical_systems;
-        (*kinetic_reactant.amount_avg)[ele_id] = amount_avg;
+        (*kinetic_reactant.amount_avg)[ele_id] =
+            averageReactantAmount(kinetic_reactant, chemical_system_indices);
     }
 
     for (auto& equilibrium_reactant : _chemical_system->equilibrium_reactants)
     {
-        double amount_avg = std::accumulate(
-            chemical_system_indices.begin(), chemical_system_indices.end(), 0.0,
-            [&](double amount, auto const& id) {
-                return amount + (*equilibrium_reactant.amount)[id];
-            });
-
-        amount_avg /= num_chemical_systems;
-        (*equilibrium_reactant.amount_avg)[ele_id] = amount_avg;
+        (*equilibrium_reactant.amount_avg)[ele_id] = averageReactantAmount(
+            equilibrium_reactant, chemical_system_indices);
     }
 }
 }  // namespace PhreeqcIOData