                            (when-let [row (first (apply map vector (replace-empty xs)))]
                              (when (not-every? is-sentinel? row)
                                row)))]
-    (loop [ms measurements data (list)]
+    (loop [ms measurements]
       (let [row (next-row ms)]
-        (if-not row
-          (if (seq data)
-            (apply map vector data)
-            (list))
+        (when row
           (let [row-timestamps (mapv :timestamp row)
                 row-timestamp  (first (sort (remove nil? row-timestamps)))
                 data-indexes   (indexes-of row-timestamp row-timestamps)

    (try
      (with-open [out (io/writer tmpfile)]
        (csv/write-csv out header)
        (csv/write-csv out (measurements->columns (filled-measurements data))))
      (s3/store-file (:s3 store) (-> item
                                     (assoc :dir      (.getParent tmpfile)
                                            :filename (.getName tmpfile))

                                                       (hash-map :timestamp row-timestamp :value nil))) row)
                fns            (map-indexed (fn [i _] (if (has-data? i) next-or-sentinel identity)) row)
                step           (fn [xs] (map (fn [f v] (f v)) fns xs))]
            (csv/write-csv out (map measurements->columns out-row))
            (recur (step ms))))))))


(defn generate-file [store item]
  (let [{:keys [header data]} item
