public Node simplify(Node n)
	 {
		 leafNodes.clear();
		 matches.clear();
		 n.clearMarks();
		 
		 if(!q_simplify(n))			//if it can not be simplified with this DynamicFormula, return as it is
		 	return n;
		 	
		 Node target = match_node;		//get Node where the match is found
		 
		 if( target.equals(n) )			//if match was found at root
		 {
			 Node ret = new Node(state2);			//take other state
			 substituteValues(ret);			//put the values of this variables in it
			 
			 ArrayList<Node> temp = unused_simplify(target);		//make a list of unused children
			 
			 if(state2.data.equals(n.data) || temp.size() == 0)			//if new root operator is same as old, merge em
			 {
                 for (Node aTemp : temp) {
                     ret.child.add(aTemp);            //make all unused children, the children of ret
                 }

                 Parser.condense(ret);
				 ArithmeticSolver.simplifySolve(ret);			//apply simplifySolve() before returning

				 return ret;
			 }
			 else								//otherwise, make new root a children of the old
			 {
				 Node ret2 = new Node();
				 ret2.data = n.data;
				 ret2.child.add(ret);
                 for (Node aTemp : temp) {
                     ret2.child.add(aTemp);            //make all unused children, the children of ret
                 }
				 ArithmeticSolver.simplifySolve(ret2);			//apply simplifySolve() before returning
				 Parser.condense(ret2);
				 return ret2;
			 }
		 }
		 else
		 {
			 Node p_targ = findParent(n, target);
			 for(int j = 0; j<p_targ.child.size(); j++)			//loop through parent's kids
			 {
				  if(p_targ.child.get(j).equals(target))	//find target
				  {
					  ArrayList<Node> temp = unused_simplify(target);
					  
					  p_targ.child.set(j, new Node(state2));
					  substituteValues(p_targ.child.get(j));

                      for (Node aTemp : temp) {
                          p_targ.child.get(j).child.add(aTemp);
                      }
				      ArithmeticSolver.simplifySolve(n);
				      Parser.condense(n);
				      return n;
				  }
			 }
		 }
		 ArithmeticSolver.simplifySolve(n);
		 Parser.condense(n);
		 return n;
	 }