add test for nested functions
diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp
index b22e04d..f75c5bd 100644
--- a/include/inja/parser.hpp
+++ b/include/inja/parser.hpp
@@ -288,10 +288,13 @@
             func->callback = function_data.callback;
           }
 
-          function_stack.pop();
-          
+          if (operator_stack.empty()) {
+            throw_parser_error("internal error at function " + func->name);
+          }
+
           current_expression_list->rpn_output.emplace_back(operator_stack.top());
           operator_stack.pop();
+          function_stack.pop();
         }
       }
       default:
diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp
index 45595d0..9919003 100644
--- a/single_include/inja/inja.hpp
+++ b/single_include/inja/inja.hpp
@@ -3000,10 +3000,13 @@
             func->callback = function_data.callback;
           }
 
-          function_stack.pop();
-          
+          if (operator_stack.empty()) {
+            throw_parser_error("internal error at function " + func->name);
+          }
+
           current_expression_list->rpn_output.emplace_back(operator_stack.top());
           operator_stack.pop();
+          function_stack.pop();
         }
       }
       default:
diff --git a/test/test-functions.cpp b/test/test-functions.cpp
index d339e4c..d233789 100644
--- a/test/test-functions.cpp
+++ b/test/test-functions.cpp
@@ -223,6 +223,11 @@
     return number1 * number2 * number3;
   });
 
+  env.add_callback("length", 1, [](inja::Arguments args) {
+    auto number1 = args.at(0)->get<std::string>();
+    return number1.length();
+  });
+
   env.add_callback("multiply", 0, [](inja::Arguments args) { return 1.0; });
 
   CHECK(env.render("{{ double(age) }}", data) == "56");
@@ -230,6 +235,8 @@
   CHECK(env.render("{{ double-greetings }}", data) == "Hello Hello!");
   CHECK(env.render("{{ double-greetings() }}", data) == "Hello Hello!");
   CHECK(env.render("{{ multiply(4, 5) }}", data) == "20.0");
+  CHECK(env.render("{{ multiply(length(\"tester\"), 5) }}", data) == "30.0");
+  CHECK(env.render("{{ multiply(5, length(\"t\")) }}", data) == "5.0");
   CHECK(env.render("{{ multiply(3, 4, 5) }}", data) == "60.0");
   CHECK(env.render("{{ multiply }}", data) == "1.0");
 
@@ -268,6 +275,7 @@
   CHECK(env.render("{{ last(list_of_objects).d * 2}}", data) == "10");
   CHECK(env.render("{{ last(range(5)) * 2 }}", data) == "8");
   CHECK(env.render("{{ last(range(5 * 2)) }}", data) == "9");
+  CHECK(env.render("{{ last(range(5 * 2)) }}", data) == "9");
   CHECK(env.render("{{ not true }}", data) == "false");
   CHECK(env.render("{{ not (true) }}", data) == "false");
   CHECK(env.render("{{ true or (true or true) }}", data) == "true");