       componentMap[name] = message.payload.component
 
   for message in messages
-    if message.protocol != 'graph'
-      throw new Error "Unknown FBP runtime sub-protocol #{message.protocol}"
-    else if message.command == 'addnode'
-      index = addNode message.payload, componentLib, buffer, index
-    else if message.command == 'addedge'
-      index = addEdge message.payload, componentLib, nodeMap, componentMap, buffer, index
-    else if message.command == 'addinitial'
-      index = addInitial message.payload, componentLib, nodeMap, componentMap, buffer, index
-    else
-      throw new Error "Unknown FBP runtime graph command #{message.command}" 
+    index = toCommandStreamBuffer message, componentLib, nodeMap, componentMap, buffer, index
 
   r =
     index: index - startIndex
     nodeId: currentNodeId
   return r
 
-# TODO: actually add observers to graph, and emit a command stream for the changes
-
 cmdStreamFromGraph = (componentLib, graph, debugLevel, openclose) ->
   debugLevel = debugLevel or 'Error'
-  buffer = new Buffer(1024)
-  # FIXME: unhardcode
+  buffer = new Buffer(10*1024) # FIXME: unhardcode
   index = 0
   currentNodeId = 1
   if !graph.nodeMap
     graph.nodeMap = {}
-  # Start comm
-  if openclose
-    index += writeString(buffer, index, cmdFormat.magicString)
-  # Clear existing graph
-  index += writeCmd(buffer, index, cmdFormat.commands.Reset.id)
-  # Config
-  index += writeCmd(buffer, index, cmdFormat.commands.ConfigureDebug.id, cmdFormat.debugLevels[debugLevel].id)
-  # Actual graph
-  r = cmdStreamBuildGraph(currentNodeId, buffer, index, componentLib, graph)
-  index += r.index
-  currentNodeId = r.nodeId
-  # Start the network
-  index += writeCmd(buffer, index, cmdFormat.commands.StartNetwork.id)
-  if openclose
-    index += writeCmd(buffer, index, cmdFormat.commands.End.id)
+  graphName = 'default'
+  
+  graph.componentMap = {}
+
+  messages = initialGraphMessages graph, graphName, debugLevel, openclose
+  r = cmdStreamBuildGraph messages, currentNodeId, buffer, index, componentLib, graph.nodeMap, graph.componentMap
+  index = r.index
+
   buffer = buffer.slice(0, index)
-  buffer
+  return buffer
 
 nodeNameById = (nodeMap, wantedId) ->
   for name of nodeMap

  index += writeCmd buffer, index, cmdBuf
  return index

cmdStreamBuildGraph = (currentNodeId, buffer, index, componentLib, graph) ->
  messages = protocol.graphToFbpMessages graph, 'default'

  nodeMap = graph.nodeMap
  startIndex = index

  componentMap = {}
  for message in messages
    if message.command == 'addnode'
      name = message.payload.id

  r = if parent != undefined then graph.processes[nodeNameById(graph.nodeMap, parent)].graph.processes else graph.processes
  r[nodeName]

# TODO: move each parsing into individual function and merge logic deviceResponseToFbpProtocol
# to return a FBP runtime message directly. Should then be the inverse of toCommandStreamBuffer/addNode/addInitial etc.
parseReceivedCmd = (componentLib, graph, cmdData, handler) ->
  `var targetPort`
  `var targetNode`
