diff --git a/src/ExprNode.cc b/src/ExprNode.cc
index 7862741404477bf258819377aa481daa20a4ac98..31c3bbefeb6715f13e9a72dd4d6c3cf668b2421f 100644
--- a/src/ExprNode.cc
+++ b/src/ExprNode.cc
@@ -5323,7 +5323,7 @@ BinaryOpNode::substituteUnaryOpNodes(DataTree &static_datatree, diff_table_t &no
 int
 BinaryOpNode::countDiffs() const
 {
-  return arg1->countDiffs() + arg2->countDiffs();
+  return max(arg1->countDiffs(), arg2->countDiffs());
 }
 
 expr_t
@@ -6585,7 +6585,7 @@ TrinaryOpNode::substituteUnaryOpNodes(DataTree &static_datatree, diff_table_t &n
 int
 TrinaryOpNode::countDiffs() const
 {
-  return arg1->countDiffs() + arg2->countDiffs() + arg3->countDiffs();
+  return max(arg1->countDiffs(), max(arg2->countDiffs(), arg3->countDiffs()));
 }
 
 expr_t
@@ -7069,7 +7069,7 @@ AbstractExternalFunctionNode::countDiffs() const
 {
   int ndiffs = 0;
   for (auto argument : arguments)
-    ndiffs += argument->countDiffs();
+    ndiffs = max(ndiffs, argument->countDiffs());
   return ndiffs;
 }
 
diff --git a/src/ExprNode.hh b/src/ExprNode.hh
index f44f9d06471c1a56e68d57db3a7589cf00acaf7d..d65b3d27bedca067e63e0f746aa6551546cfed3d 100644
--- a/src/ExprNode.hh
+++ b/src/ExprNode.hh
@@ -518,7 +518,7 @@ class ExprNode
       //! Returns true if the expression contains one or several exogenous variable
       virtual bool containsExogenous() const = 0;
 
-      //! Returns the number of diffs present
+      //! Returns the maximum number of nested diffs in the expression
       virtual int countDiffs() const = 0;
 
       //! Return true if the nodeID is a variable withe a type equal to type_arg, a specific variable id aqual to varfiable_id and a lag equal to lag_arg and false otherwise