                         "payments" [])))
 
 (defn data [options]
-  (println (json/write-str (map add-convenience-fields
-                                (filter (options-to-filter options)
-                                        (read-all-invoices))))))
+  (println (json/write-str (map serialize-invoice
+                                (map remove-convenience-fields ;; TODO: not have to do that
+                                     (sort compare-invoices-by-issue-date
+                                           (filter (options-to-filter options)
+                                                   (read-all-invoices))))))))
 
 (defn list_ [options]
   (pprint/print-table (map pretty-print-invoice
-                           (filter (options-to-filter options)
-                                   (read-all-invoices)))))
+                           (sort compare-invoices-by-issue-date
+                                 (filter (options-to-filter options)
+                                         (read-all-invoices))))))
 
 (defn record-payment [options arguments]
   (let [id (get arguments 1)

(defn id-to-filename [id]
  (str id ".json"))

(defn read-invoice [id]
  (read-json (id-to-filename id)))

(defn write-invoice [id value]
  (write-json (id-to-filename id) value))

(defn get-invoice-filenames []
  (filter #(.endsWith % ".json") (map #(.getName %) (file-seq (io/file (cwd))))))

(defn read-all-invoices []
  (map read-json (get-invoice-filenames)))

(defn sum-payments [invoice]
  (sum (map #(get % "amount") (get invoice "payments"))))

(defn pretty-print-invoice [invoice]
  {"ID" (get invoice "id")
   "From" (get invoice "from")
   "To" (get invoice "to")
   "Issue date" (get invoice "issue-date")
   "Due date" (get invoice "due-date")
   "Amount" (format-amount (get invoice "amount") (get invoice "currency"))
   "Paid" (format-amount (sum-payments invoice)
                         (get invoice "currency"))})

(defn add-convenience-fields [invoice]
  (assoc invoice "paid" (sum-payments invoice)))

(defn options-to-filter [options]
  (fn [invoice]

            [clojure.java.io :as io]
            [clj-time.core :as t]
            [clj-time.format :as f]
            [clj-time.coerce :as c]
            [clojure.pprint :as pprint]
            [clojure.string :as string])
  (:gen-class))
