 includes/math-expr.inc | 37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/includes/math-expr.inc b/includes/math-expr.inc
index beffb93..7153cb8 100644
--- a/includes/math-expr.inc
+++ b/includes/math-expr.inc
@@ -215,19 +215,19 @@ class ctools_math_expr {
                     else $output[] = $o2;
                 }
                 if (preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches)) { // did we just close a function?
-                    $fnn = $matches[1]; // get the function name
-                    $arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
-                    $output[] = $stack->pop(); // pop the function and push onto the output
-                    if (in_array($fnn, $this->fb)) { // check the argument count
-                        if($arg_count > 1)
-                            return $this->trigger("too many arguments ($arg_count given, 1 expected)");
-                    } elseif (array_key_exists($fnn, $this->f)) {
-                        if ($arg_count != count($this->f[$fnn]['args']))
-                            return $this->trigger("wrong number of arguments ($arg_count given, " . count($this->f[$fnn]['args']) . " expected)");
-                    } else { // did we somehow push a non-function on the stack? this should never happen
+                  $fnn = $matches[1]; // get the function name
+                  $arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
+                  $output[] = $stack->pop(); // pop the function and push onto the output
+                  if (!in_array($fnn, $this->fb)){
+                    if (array_key_exists($fnn, $this->f)) {
+                      if ($arg_count != count($this->f[$fnn]['args']))
+                        return $this->trigger("wrong number of arguments ($arg_count given, " . count($this->f[$fnn]['args']) . " expected)");
+                      }
+                      else { // did we somehow push a non-function on the stack? this should never happen
                         return $this->trigger("internal error");
+                      }
                     }
-                }
+                  }
                 $index++;
             //===============
             } elseif ($op == ',' and $expecting_op) { // did we just finish a function argument?
@@ -324,10 +324,17 @@ class ctools_math_expr {
             } elseif (preg_match("/^([a-z]\w*)\($/", $token, $matches)) { // it's a function!
                 $fnn = $matches[1];
                 if (in_array($fnn, $this->fb)) { // built-in function:
-                    if (is_null($op1 = $stack->pop())) return $this->trigger("internal error");
-                    $fnn = preg_replace("/^arc/", "a", $fnn); // for the 'arc' trig synonyms
-                    if ($fnn == 'ln') $fnn = 'log';
-                    eval('$stack->push(' . $fnn . '($op1));'); // perfectly safe eval()
+                  if ($stack->count>1){//Support more than 2 arguments
+                    //Pop all elements to build op1
+                    $op1 = implode(',', array_slice($stack->stack, 0, $stack->count));
+                      $stack->count = 0;//Reset counter/Empty stack
+                  }
+                  elseif (is_null($op1 = $stack->pop())) {
+                    return $this->trigger("internal error");
+                  }
+                  $fnn = preg_replace("/^arc/", "a", $fnn); // for the 'arc' trig synonyms
+                  if ($fnn == 'ln') $fnn = 'log';
+                  eval('$stack->push(' . $fnn . '('.$op1.'));'); // perfectly safe eval()
                 } elseif (array_key_exists($fnn, $this->f)) { // user function
                     // get args
                     $args = array();
