         tabSize: 4
     })
 
-    expose.doc = cmTaskpaper.editor.doc
-    expose.generation = expose.doc.changeGeneration()
-    expose.doc.on 'change', () =>
+    # Initialize CodeMirror document
+    doc = cmTaskpaper.editor.doc
+    doc.on 'change', () =>
         amplify.publish 'outline-changed', doc.changeGeneration(), 'doc.change'
-
     cmTaskpaper.loadTarget './todo.taskpaper', () ->
         contents = doc.getValue()
-        expose.generation = expose.doc.changeGeneration()
-        setOutline(contents)
+
+    # Initialize outline
+    outline = new birch.Outline.createTaskPaperOutline(doc.getValue())
+    outline.generation = doc.changeGeneration()
+    outline.onDidEndChanges () ->
+        outline.generation++
+        amplify.publish 'outline-changed', outline.generation, 'outline.onDidEndChanges'
 
     cmTaskpaper.title.text = 'CodeMirror View'
-
     panel.insertLeft(cmTaskpaper)
     panel.insertBottom(coffeeconsole, cmTaskpaper)
     panel.attach(document.body)

    amplify.subscribe 'outline-changed', (generation, source) ->
        # console.log 'outline-changed', generation, source
        if not doc.isClean(generation)
            expose.doc.setValue(outline.serialize())
        if generation > expose.generation and expose.doc and expose.outline
            setOutline(expose.doc.getValue())

window.onload = main

import './index.css'


#
# Inject a method to load a file via AJAX.
#
