Index: /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs
===================================================================
--- /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs	(revision 14539)
+++ /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs	(revision 14540)
@@ -237,13 +237,13 @@
       var t = (BinaryFactorVariableTreeNode)binFactorVar.CreateTreeNode();
       var varNameTok = tokens.Dequeue();
-      Debug.Assert(tok.Symbol == TokenSymbol.SYMB);
+      Debug.Assert(varNameTok.Symbol == TokenSymbol.SYMB);
       t.VariableName = varNameTok.StringValue;
 
       var varValTok = tokens.Dequeue();
-      Debug.Assert(tok.Symbol == TokenSymbol.SYMB);
+      Debug.Assert(varValTok.Symbol == TokenSymbol.SYMB);
       t.VariableValue = varValTok.StringValue;
 
       var weightTok = tokens.Dequeue();
-      Debug.Assert(tok.Symbol == TokenSymbol.NUMBER);
+      Debug.Assert(weightTok.Symbol == TokenSymbol.NUMBER);
       t.Weight = weightTok.DoubleValue;
 
Index: /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
===================================================================
--- /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs	(revision 14539)
+++ /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs	(revision 14540)
@@ -24,4 +24,5 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
 using HeuristicLab.Common;
@@ -64,4 +65,10 @@
       ISymbolicExpressionTreeNode rootNode = (new ProgramRootSymbol()).CreateTreeNode();
       rootNode.AddSubtree(GetSimplifiedTree(macroExpandedTree));
+
+#if DEBUG
+      // check that each node is only referenced once
+      var nodes = rootNode.IterateNodesPrefix().ToArray();
+      foreach(var n in nodes) if(nodes.Count(ni => ni == n) > 1) throw new InvalidOperationException();
+#endif
       return new SymbolicExpressionTree(rootNode);
     }
@@ -904,5 +911,5 @@
         return a.Subtrees
           .Select(x => GetSimplifiedTree(x))
-          .Select(x => MakeFraction(x, b))
+          .Select(x => MakeFraction(x, GetSimplifiedTree(b)))
           .Aggregate((c, d) => MakeSum(c, d));
       } else if(IsMultiplication(a) && IsConstant(b)) {
@@ -1084,6 +1091,5 @@
       if(IsConstant(a) && IsConstant(b)) {
         // fold constants
-        ((ConstantTreeNode)a).Value *= ((ConstantTreeNode)b).Value;
-        return a;
+        return MakeConstant(((ConstantTreeNode)a).Value * ((ConstantTreeNode)b).Value);
       } else if(IsConstant(a)) {
         // a * $ => $ * a
@@ -1123,7 +1129,8 @@
         return a;
       } else if(IsConstant(b) && IsAddition(a) ||
-          IsFactor(b) && IsAddition(a)) {
+          IsFactor(b) && IsAddition(a) ||
+          IsBinFactor(b) && IsAddition(a)) {
         // multiply constants into additions
-        return a.Subtrees.Select(x => MakeProduct(x, b)).Aggregate((c, d) => MakeSum(c, d));
+        return a.Subtrees.Select(x => MakeProduct(GetSimplifiedTree(x), GetSimplifiedTree(b))).Aggregate((c, d) => MakeSum(c, d));
       } else if(IsDivision(a) && IsDivision(b)) {
         // (a1 / a2) * (b1 / b2) => (a1 * b1) / (a2 * b2)
@@ -1146,5 +1153,5 @@
       } else if(IsMultiplication(a)) {
         // a is already an multiplication => append b
-        a.AddSubtree(b);
+        a.AddSubtree(GetSimplifiedTree(b));
         MergeVariablesAndConstantsInProduct(a);
         return a;
@@ -1256,5 +1263,5 @@
     /// <summary>
     /// x => x * -1
-    /// Doesn't create new trees and manipulates x
+    /// Is only used in cases where it is not necessary to create new tree nodes. Manipulates x directly.
     /// </summary>
     /// <param name="x"></param>
@@ -1293,5 +1300,5 @@
     /// <summary>
     /// x => 1/x
-    /// Doesn't create new trees and manipulates x
+    /// Must create new tree nodes
     /// </summary>
     /// <param name="x"></param>
@@ -1302,6 +1309,5 @@
       } else if(IsFactor(x)) {
         var factorNode = (FactorVariableTreeNode)x;
-        for(int i = 0; i < factorNode.Weights.Length; i++) factorNode.Weights[i] = 1.0 / factorNode.Weights[i];
-        return factorNode;
+        return MakeFactor(factorNode.Symbol, factorNode.VariableName, factorNode.Weights.Select(w => 1.0 / w));
       } else if(IsDivision(x)) {
         return MakeFraction(x.GetSubtree(1), x.GetSubtree(0));
