 
 makeStack = (uid) ->
 	stack = []
-	pre = []
-	read = []
-	int = []
-	post = []
-	exit = false
-
-	stackish = {}
-
-	stackish.push = ->
+	declarations = []
+	reads = []
+	writes = []
+	chunks = []
+	exitRequest = false
+
+	stackObj = {}
+
+	stackObj.push = ->
 		Array::push.apply stack, arguments
 		return
 
-	stackish.popish = ->
+	stackObj.pop = ->
 		if stack.length > 0
 			stack.pop()
 		else
-			name = "p#{uid}_#{pre.length}"
+			name = "p#{uid}_#{declarations.length}"
 			# use const once node supports it
-			pre.push "var #{name} = programState.pop()"
+			declarations.push "var #{name} = programState.pop()"
 			name
 
-	stackish.peek = ->
+	stackObj.peek = ->
 		if stack.length
 			stack[stack.length - 1]
 		else
-			name = "p#{uid}_#{pre.length}"
+			name = "p#{uid}_#{declarations.length}"
 			# use const once node supports it
-			pre.push "var #{name} = programState.peek()"
+			declarations.push "var #{name} = programState.peek()"
 			name
 
 	makeNext = (methodName) ->
 		->
-			name = "r#{uid}_#{read.length}"
+			name = "r#{uid}_#{reads.length}"
 			# use const once node supports it
-			read.push "var #{name} = programState.#{methodName}()"
+			reads.push "var #{name} = programState.#{methodName}()"
 			name
 
-	stackish.next = makeNext 'next'
-	stackish.nextChar = makeNext 'nextChar'
-
-	stackish.out = (entry) ->
-		post.push entry
-		return
-
-	stackish.dump = ->
-		int.push """
-			#{pre.join '\n'}
-			#{read.join '\n'}
-			programState.push(#{stack.join ', '})
-			#{post.join '\n'}
+	stackObj.next = makeNext 'next'
+	stackObj.nextChar = makeNext 'nextChar'
+
+	stackObj.out = (entry) ->
+		writes.push entry
+		return
+
+	stackObj.dump = ->
+		chunks.push """
+			#{declarations.join '\n'}
+			#{reads.join '\n'}
+			#{if stack.length > 0 then "programState.push(#{stack.join ', '})" else ''}
+			#{writes.join '\n'}
 		"""
 
 		stack = []
-		pre = []
-		read = []
-		post = []
-
-		return
-
-	stackish.int = (entry) ->
-		int.push entry
-		return
-
-	stackish.stringify = ->
-		"""
-			#{int.join '\n'}
-			#{if exit then 'programState.exit()' else ''}
-		"""
-
-
-	stackish.exit = ->
-		exit = true
-
-	stackish
+		declarations = []
+		reads = []
+		writes = []
+
+		return
+
+	stackObj.pushChunk = (entry) ->
+		chunks.push entry
+		return
+
+	stackObj.stringify = ->
+		"""
+			#{chunks.join '\n'}
+			#{if exitRequest then 'programState.exit()' else ''}
+		"""
+
+	stackObj.exit = ->
+		exitRequest = true
+
+	stackObj
 
 
 StackingCompiler.assemble = (path) ->


	'p': (x, y, dir, index, stack, from, to) ->
		stack.dump()
		stack.int """
			programState.put(
				#{stack.popish()},
				#{stack.popish()},
				#{stack.popish()},
				#{x}, #{y}, '#{dir}', #{index},
				'#{from}', '#{to}'
			)



	'\\': (x, y, dir, index, stack) ->
		e1 = stack.pop()
		e2 = stack.pop()
		stack.push e1, e2
		return


	'$': (x, y, dir, index, stack) ->
		stack.pop()
		return


	'.': (x, y, dir, index, stack) ->
		stack.out("programState.out(#{stack.pop()})")
		return


	',': (x, y, dir, index, stack) ->
		stack.out("programState.out(String.fromCharCode(#{stack.pop()}))")
		return


