                               (edit-link page) (delete-link page)])])))
 
 (defn delete-page
-  "Deletes the specified page by name."
-  [name]
-    (try
-      (do (ds/delete! (ds/retrieve Page name))
-          (response-wrapper "Deleted"
-                            (str "Page deleted: " name)))
+  "Deletes the specified page by title."
+  [title]
+  (try
+    (do (ds/delete! (ds/retrieve Page title))
+        (response-wrapper "Deleted"
+                          (str "Page deleted: " title)))
       
     (catch EntityNotFoundException _
-      (response-wrapper "404" (str "Unable to find page: " name) 404))))
+      (response-wrapper "404" (str "Unable to find page: " title) 404))))
 
 (defn admin-edit-page
   "Displays the Ackbar Admin 'Edit Page' Page"
-  []
-  (result "Admin Edit Page"))
+  [title]
+  (try
+    (def page (ds/retrieve Page title))
+    (response-wrapper "Edit Page"
+                      (add-or-edit-form "/admin/edit"
+                                        (:title page)
+                                        (:body page)))
+    (catch EntityNotFoundException _
+      (response-wrapper "404" (str "Unable to find page: " title) 404))))
 
+(defn edit-page
+  "Edits a page, updating its title and content"
+  [title body]
+  (if (ds/exists? Page (canonical-title title))
+    (ds/delete! (ds/retrieve Page (canonical-title title))))
+  (ds/save! (Page. (canonical-title title) body))
+  (result "Page updated!"))
+ 
 (defroutes ackbar-app-handler
   (GET "/" [] (render-page "home"))
   (GET "/admin/add" [] (admin-add-page))
   (GET "/admin/pages" [] (admin-page-list))
   (POST "/admin/add" {params :params}
         (add-page (params "title") (params "body")))
-  (GET "/admin/edit/:name" [name] (admin-edit-page name))
-  (POST "/admin/edit/:name" [name params] (render-page "edit page post"))
-  (POST "/admin/delete/:name" [name] (delete-page name))
-  (GET "/:name" [name] (render-page (canonical-name name)))
+  (GET "/admin/edit/:title" [title] (admin-edit-page title))
+  (POST "/admin/edit" {params :params}
+        (edit-page (params "title") (params "body")))
+  (POST "/admin/delete/:title" [title] (delete-page title))
+  (GET "/:title" [title] (render-page (canonical-title title)))
   (ANY "*" [] (response-wrapper "404" "NOT FOUND!" 404)))
 
 (ae/def-appengine-app ackbar-app #'ackbar-app-handler)


(defn render-page
  "Retrieve a page from the datastore and render it for display to the user"
  [name]
  (try
    (response-wrapper name (:body (ds/retrieve Page name)))
    (catch EntityNotFoundException _
      (response-wrapper "404" (str "Unable to find page: " name) 404))))

(defn admin-add-page
  "Displays the Ackbar Admin 'Add Page' Page"
  []
  (response-wrapper "Add Page"
                    (form-to [:post "/admin/add"]
                             (label "title" "Title:")
                             [:br]
                             (text-field "title")
                             [:br]
                             (label "body" "Body:")
                             [:br]
                             (text-area {:cols 80 :rows 40} "body")
                             [:br]
                             (submit-button "Submit")
                             )))

(defn add-page
  "Adds a page to the database with the specified title and content"
  [title body]
  (ds/save! (Page. (canonical-name title) body))
  (result "Page saved!")
  )

(defn page-link
  "Builds a link to the specified page object"
  [page]
  (link-to (str "/" (:name page)) (:name page)))

(defn edit-link
  "Builds a link to edit the specified page"
  [page]
  (link-to (str "/admin/edit/" (:name page)) "edit"))

(defn delete-link
  "Builds a link to delete the specified page"
  [page]
  (form-to [:post (str "/admin/delete/" (:name page))]
           (submit-button "Delete")))

(defn admin-page-list

            [clojure.contrib.str-utils2 :as s])
  (:import com.google.appengine.api.datastore.EntityNotFoundException))

(ds/defentity Page [^:key title, body])

(defn canonical-title
  "Converts a given page title to a standard format, suitable for use in a URL."
  [str]
  (s/replace (s/replace (.toLowerCase str) #"\s+" "_") #"\W+" ""))

(defn wrap-result [result]
  {:status 200
