Fixed flattening of merged groups
diff --git a/public/js/spec/vcSpec.js b/public/js/spec/vcSpec.js
index dbb60b0..4223e46 100644
--- a/public/js/spec/vcSpec.js
+++ b/public/js/spec/vcSpec.js
@@ -831,6 +831,10 @@
   it('should be modifiable by deletion in nested docGroups (root case)', function () {
     var vc = nestedGroupFactory.create();
 
+    expect(vc.toString()).toEqual(
+      'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
+    );
+
     var docGroup = vc.root();
     expect(docGroup.ldType()).toEqual("docGroup");
     expect(docGroup.operation()).toEqual("or");
@@ -860,8 +864,15 @@
 	vc.root().getOperand(0)
       ).update().ldType()
     ).toEqual("docGroup");
+
     expect(vc.root().ldType()).toEqual("docGroup");
     expect(vc.root().operation()).toEqual("and");
+    expect(vc.root().getOperand(0).ldType()).toEqual("doc");
+
+    expect(vc.toString()).toEqual(
+      'pubDate since 2014-05-12 & pubDate until 2014-12-05'
+    );
+
   });
 
   it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
@@ -891,12 +902,17 @@
 
     // Get nested group
     var firstGroup = vc.root().getOperand(1);
-    firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
+    firstGroup.append(simpleGroupFactory.create({
+      "operation" : "operation:or"
+    }));
+    var oldAuthor = firstGroup.getOperand(2).getOperand(0);
+    oldAuthor.key("title");
+    oldAuthor.value("Der Birnbaum");
     
     // Structur is now:
     // or(doc, and(doc, doc, or(doc, doc)))
     expect(vc.toString()).toEqual(
-      'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & (author = "Max Birkendale" | pubDate in 2014-12-05))'
+      'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & (title = "Der Birnbaum" | pubDate in 2014-12-05))'
     );
 
     var andGroup = vc.root().getOperand(1);
@@ -915,7 +931,7 @@
     // or(doc, and(doc, or(doc, doc)))
 
     expect(vc.toString()).toEqual(
-      'author = "Max Birkendale" | (pubDate since 2014-05-12 & (author = "Max Birkendale" | pubDate in 2014-12-05))'
+      'author = "Max Birkendale" | (pubDate since 2014-05-12 & (title = "Der Birnbaum" | pubDate in 2014-12-05))'
     );
 
 
@@ -925,7 +941,7 @@
     // or(doc, doc, doc)
 
     expect(vc.toString()).toEqual(
-      'author = "Max Birkendale" | author = "Max Birkendale" | pubDate in 2014-12-05'
+      'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
     );
   });
 });
diff --git a/public/js/src/vc.js b/public/js/src/vc.js
index a713b09..6cb3925 100644
--- a/public/js/src/vc.js
+++ b/public/js/src/vc.js
@@ -80,16 +80,17 @@
 	obj._root = KorAP.UnspecifiedDoc.create(obj);
       };
 
-      // Add root element to root node
-      obj.element().appendChild(
-	obj._root.element()
-      );
+      // Init element and update
+      obj.update();
 
       return obj;
     },
     root : function (obj) {
-      if (arguments.length === 1)
+      if (arguments.length === 1) {
+console.log("Set vc root to " + obj.toString());
 	this._root = obj;
+	this.update();
+      };
       return this._root;
     },
     element : function () {
@@ -98,6 +99,7 @@
 
       this._element = document.createElement('div');
       this._element.setAttribute('class', 'vc');
+
       return this._element;
     },
     toJson : function () {
@@ -105,6 +107,13 @@
     },
     toString : function () {
       return this._root.toString();
+    },
+    update : function () {
+      var e = this.element();
+      if (e.firstChild !== null)
+	e.replaceChild(this._root.element(), e.firstChild);
+      else
+	e.appendChild(this._root.element());
     }
   };
 
@@ -585,22 +594,25 @@
     },
     update : function () {
 
+      console.log("Update");
+
       // There is only one operand in group
       if (this._operands.length === 1) {
 	var parent = this.parent();
+	var op = this.getOperand(0);
+
+	console.log("There is only one operand left in the group");
 
 	// Parent is a group
 	if (parent.ldType() !== null) {
-	  return parent.replaceOperand(
-	    this,
-	    this.getOperand(0)
-	  ).update();
+	  console.log("The group is not directly below root");
+	  return parent.replaceOperand(this, op).update();
 	}
 
 	// Parent is vc root
 	else {
-console.log("parent is vc");
-	  parent.root(this.getOperand(0));
+	  console.log("The group is directly below root");
+	  parent.root(op);
 	  this.destroy();
 	  return parent.root();
 	};
@@ -667,26 +679,28 @@
 
     // Replace operand
     replaceOperand : function (oldOp, newOp) {
+
       for (var i in this._operands) {
 	if (this._operands[i] === oldOp) {
 
 	  // Just insert a doc
 	  if (newOp.ldType() === "doc") {
-	    console.log("Insert doc in group");
 	    this._operands[i] = newOp;
 	  }
 	  // Insert a group of a different operation
 	  // (i.e. "and" in "or"/"or" in "and")
-	  else if (newOp.operation() != oldOp.operation()) {
-	    console.log("Insert group in group - no flatten");
+	  else if (newOp.operation() != this.operation()) {
 	    this._operands[i] = newOp;
 	  }
 
 	  // Flatten the group
 	  else {
-	    console.log("Insert group in group - flatten");
+	    // Remove old group
+	    this._operands.splice(i, 1);
+
+	    // Inject new operands
 	    for (var op in newOp.operands().reverse())
-	      this._operands.splice(i, 1, newOp.getOperand(op))
+	      this._operands.splice(i, 0, newOp.getOperand(op))
 	  };
 	  oldOp.destroy();
 	  return this;
@@ -699,6 +713,9 @@
     delOperand : function (obj) {
       for (var i in this._operands) {
 	if (this._operands[i] === obj) {
+
+	  console.log("Deleted operand " + i);
+
 	  this._operands.splice(i,1);
 
 	  // Destroy object for cyclic references