     version.stringValue = "1.0"
     version.kind
     package.addAttribute(version)
-
+    
     let metadata = XMLElement(name: "metadata")
     let title = XMLElement(name: "dc:title", stringValue: "dod")
     let creator = XMLElement(name: "dc:creator", stringValue: "Richard Fabian")
     metadata.addChild(title)
     metadata.addChild(creator)
-
+    
     package.addChild(metadata)
-
+    
     let namespace = XMLElement(kind: .namespace)
     namespace.name = ""
     namespace.stringValue = "http://www.idpf.org/2007/opf"
     package.addNamespace(namespace)
+    
+    let manifest = XMLElement(name: "manifest")
+    
+    for i in 0..<fileList.count {
+        manifest.addChild(manifestItem(withName: fileList[i], index: i))
+    }
+    
+    package.addChild(manifest)
+    
+    let itemrefs = manifest.children?.filter { child in
+        return child is XMLElement
+        } as! Array<XMLElement>
+    let ids = itemrefs.filter
+        { $0.attribute(forName: "media-type")?.stringValue == "application/xhtml" }
+        .flatMap { $0.attribute(forName: "id")?.stringValue }
+    let spine = createSpine(withItemrefs: ids)
+    package.addChild(spine)
+    
+    return root.xmlData(withOptions: Int(XMLNode.Options.nodePrettyPrint.rawValue))
+}
 
-    let manifest = XMLElement(name: "manifest")
+func manifestItem(withName name: String, index: Int) -> XMLElement {
+    let item = XMLElement(name: "item")
+    if name.hasSuffix("png") {
+        item.setAttributesAs(["href": "images/\(name)", "media-type": "image/png", "id": "id\(index)"])
+    } else {
+        item.setAttributesAs(["href": "text/\(name)", "media-type": "application/xhtml+xml", "id": "id\(index)"])
+    }
+    return item
+}
 
-    func manifestItem(withName name: String, index: Int) -> XMLElement {
-        let item = XMLElement(name: "item")
-        if name.hasSuffix("png") {
-            item.setAttributesAs(["href": "images/\(name)", "media-type": "image/png", "id": "id\(index)"])
-        } else {
-            item.setAttributesAs(["href": "text/\(name)", "media-type": "application/xhtml+xml", "id": "id\(index)"])
-        }
-        return item
-    }
+func createSpine(withItemrefs itemrefs: Array<String>) -> XMLElement {
+    let spine = XMLElement(name: "spine")
+    let toc = XMLElement(kind: .attribute)
+    toc.name = "toc"
+    toc.stringValue = "ncx"
+    spine.addChild(toc)
+    itemrefs.map { (item: String) -> XMLElement in
+        let element = XMLElement(name: "itemref")
+        let attribute = XMLElement(kind: .attribute)
+        attribute.name = "idref"
+        attribute.stringValue = item
+        return element
+        }.forEach { spine.addChild($0) }
+    
+    return spine
+}
 
-    let img = manifestItem(withName: "20.png", index: 5)
-    let text = manifestItem(withName: "20.html", index: 10)
-
-    manifest.addChild(img)
-    manifest.addChild(text)
-
-    package.addChild(manifest)
-
-    return package.xmlData(withOptions: Int(XMLNode.Options.nodePrettyPrint.rawValue))
-}
 
 let fm = FileManager.default
 

    mediaType.name = "media-type"
    mediaType.stringValue = "application/oebps-package+xml"
    rootfile.addAttribute(mediaType)


    rootfiles.addChild(rootfile)
    container.addChild(rootfiles)

    root.version = "1.0"
    root.isStandalone = true

    return root.xmlData(withOptions: Int(XMLNode.Options.nodePrettyPrint.rawValue))
}

func packageXMLData() -> Data {
    let package = XMLElement(name: "package")
    let root = XMLDocument(rootElement: package)
    root.version = "1.0"


if contents.isEmpty == false {
    dump("removing \(contents.count) previous items")
    
    for item in contents{
        try! fm.removeItem(at: item)
    }
