   #
   # This function has UNDEFINED BEHAVIOUR if you call append before calling create().
   @writeSnapshot = (docName, data, dbMeta, callback) ->
-    checkIndex (err) ->
+    client.collection 'docs', (err, collection) ->
       return callback? err if err
-      client.collection 'docs', (err, collection) ->
+      
+      doc =
+        _id: docName
+        data: data
+      
+      collection.update {_id: docName}, doc, safe: true, (err, doc) ->
+        console.warn "failed to save snapshot for doc #{docName}: #{err}" if err
         return callback? err if err
-      
-        doc =
-          docName: docName
-          data: data
-      
-        collection.update {docName: docName}, doc, safe: true, (err, doc) ->
-          console.warn "failed to save snapshot for doc #{docName}: #{err}" if err
-          return callback? err if err
         
-          callback?()
+        callback?()
       
   # Data = {v, snapshot, type}. Error if the document doesn't exist.
   @getSnapshot = (docName, callback) ->
-    checkIndex (err) ->
+    client.collection 'docs', (err, collection) ->
       return callback? err if err
-      client.collection 'docs', (err, collection) ->
+      
+      collection.findOne { _id: docName }, (err, doc) ->
         return callback? err if err
-      
-        collection.findOne { docName: docName }, (err, doc) ->
-          return callback? err if err
-          if doc != null
-            callback null, doc.data
-          else
-            callback "Document does not exist"
+        if doc != null
+          callback null, doc.data
+        else
+          callback "Document does not exist"
         
   # Permanently deletes a document. There is no undo.
   @delete = (docName, dbMeta, callback) ->
-    checkIndex (err) ->
+    client.collection 'ops', (err, collection) ->
+      collection.remove { docName: docName }
+
+    client.collection 'docs', (err, collection) ->
       return callback? err if err
-      client.collection 'ops', (err, collection) ->
-        collection.remove { docName: docName }
-
-      client.collection 'docs', (err, collection) ->
+      
+      collection.remove { _id: docName }, safe: true, (err, count) ->
         return callback? err if err
-      
-        collection.remove { docName: docName }, safe: true, (err, count) ->
-          return callback? err if err
-          if count == 0
-            callback? "Document does not exist" 
-          else
-            callback? null
+        if count == 0
+          callback? "Document does not exist" 
+        else
+          callback? null
 
   # Close the connection to the database
   @close = ->

  # end can be null. If so, returns all documents from start onwards.
  # Each document returned is in the form {op:o, meta:m, v:version}.
  @getOps = (docName, start, end, callback) ->
    checkIndex (err) ->
      return callback? err if err
      if start == end
        callback null, []
        return

      client.collection 'ops', (err, collection) ->
        return callback? err if err
      
        query = 
          docName: docName
          v:
            $gte: start
        query.v.$lt = end if end
      
        collection.find(query).sort('v').toArray (err, docs) ->
          console.warn "failed to get ops for #{docName}: #{err}" if err
          return callback? err if err

          callback? null, (doc.opData for doc in docs)
        
  # Write an op to a document.
  #

  # ... and that would slow it down a bit.)
  @writeOp = (docName, opData, callback) ->
    # ****** NOT SAFE FOR MULTIPLE PROCESSES. Rewrite me using transactions or something.
    client.collection 'ops', (err, collection) ->
      return callback? err if err
      
      doc = 
        docName: docName
        opData:
          op: opData.op
          meta: opData.meta
        v: opData.v
      
      collection.insert doc, safe: true, (err, doc) ->
        console.warn "failed to save op #{opData} for #{docName}: #{err}" if err
        return callback? err if err

        callback? null, doc
      
  # Write new snapshot data to the database.
  #
