Fixed flattening of merged groups
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