 
     @checkTransactionStart()
 
+    toRemove = []
     for edge in @edges
-      continue unless edge
-      if edge.from.node is node.id
-        @removeEdge edge.from.node, edge.from.port
-      if edge.to.node is node.id
-        @removeEdge edge.to.node, edge.to.port
-
+      if (edge.from.node is node.id) or (edge.to.node is node.id)
+        toRemove.push edge
+    for edge in toRemove
+      @removeEdge edge.from.node, edge.from.port, edge.to.node, edge.to.port
+
+    toRemove = []
     for initializer in @initializers
-      continue unless initializer
       if initializer.to.node is node.id
-        @removeInitial initializer.to.node, initializer.to.port
-
+        toRemove.push initializer
+    for initializer in toRemove
+      @removeInitial initializer.to.node, initializer.to.port
+
+    toRemove = []
     for exported in @exports
       if id.toLowerCase() is exported.process
-        @removeExports exported.public
-
+        toRemove.push exported
+    for exported in toRemove
+      @removeExports exported.public
+
+    toRemove = []
     for pub, priv of @inports
       if priv.process is id
-        @removeInport pub
+        toRemove.push pub
+    for pub in toRemove
+      @removeInport pub
+
+    toRemove = []
     for pub, priv of @outports
       if priv.process is id
-        @removeOutport pub
+        toRemove.push pub
+    for pub in toRemove
+      @removeOutport pub
 
     for group in @groups
       continue unless group

  removeInitial: (node, port) ->
    @checkTransactionStart()

    for edge, index in @initializers
      continue unless edge
      if edge.to.node is node and edge.to.port is port
        @emit 'removeInitial', edge
        @initializers.splice index, 1

    @checkTransactionEnd()


  removeEdge: (node, port, node2, port2) ->
    @checkTransactionStart()

    toRemove = []
    toKeep = []
    if node2 and port2
      for edge,index in @edges
        if edge.from.node is node and edge.from.port is port and edge.to.node is node2 and edge.to.port is port2
          @setEdgeMetadata edge.from.node, edge.from.port, edge.to.node, edge.to.port, {}
          toRemove.push edge
        else
          toKeep.push edge
    else
      for edge,index in @edges
        if (edge.from.node is node and edge.from.port is port) or (edge.to.node is node and edge.to.port is port)
          @setEdgeMetadata edge.from.node, edge.from.port, edge.to.node, edge.to.port, {}
          toRemove.push edge
        else
          toKeep.push edge

    @edges = toKeep
    for edge in toRemove
      @emit 'removeEdge', edge

    @checkTransactionEnd()

