 						}
 					"""
 
-					if cycledNodes.has node
-						"""
-							while (runtime.isAlive()) _#{node}: {
-								#{randomCode}
-							}
-						"""
-					else
-						randomCode
+					wrapIfLooping cycledNodes, node, randomCode
+
 				when 2
 					branch0 = df neighbours[0].to, newStack
 					branch1 = df neighbours[1].to, newStack


					# this might not be necessary if only
					# the starting node can have a single neighbour
					if cycledNodes.has node
						"""
							while (runtime.isAlive()) _#{node}: {
								#{edgeCode}
							}
						"""
					else
						edgeCode
				when 0
					'return;'



assemble = (graph) ->
	cycledNodes = new Set

	wrapIfLooping = (stack, node, code) ->
		if cycledNodes.has node
			"""
				while (runtime.isAlive()) _#{node}: {
					#{code}
				}
			"""
		else
			code


	df = (node, stack) ->
		# for debugging only
