   (matrix (mapv #(- % (proj-vec xs %)) data)))
 
 
-(defn centered-data [data]
-  (- data (mean data)))
-
-
 ; Will eventually also want to add last-pcs
 (defn powerit-pca [data n-comps & {:keys [iters start-vectors]}]
   "Find the first n-comps principal components of the data matrix; iters defaults to iters of
   power-iteration"
-  (let [cntrd-data (centered-data data)
+  (let [center (mean data)
+        cntrd-data (- data center)
         start-vectors (or start-vectors [])
         data-dim (min (row-count cntrd-data) (column-count cntrd-data))]
-    (loop [data' cntrd-data n-comps' (min n-comps data-dim) pcs [] start-vectors start-vectors]
-      (let [pc (power-iteration data' iters (first start-vectors)) ; may eventually want to return eigenvals...
-            pcs (conj pcs pc)]
-        (if (= n-comps' 1)
-          pcs ; return if done
-          (let [data' (factor-matrix data' pc)
-                n-comps' (dec n-comps')]
-            (recur data' n-comps' pcs (rest start-vectors))))))))
+    {:center center
+     :comps
+        (loop [data' cntrd-data n-comps' (min n-comps data-dim) pcs [] start-vectors start-vectors]
+          ; may eventually want to return eigenvals...
+          ;(debug-repl)
+          (let [start-vector (or (first start-vectors)
+                                 (matrix (for [x (range (dimension-count data' 1))] (rand))))
+                pc (power-iteration data' iters start-vector)
+                pcs (conj pcs pc)]
+            (if (= n-comps' 1)
+              pcs ; return if done
+              (let [data' (factor-matrix data' pc)
+                    n-comps' (dec n-comps')]
+                (recur data' n-comps' pcs (rest start-vectors))))))}))
 
 
-;(defn wrapped-pca [data n-comps & {:keys [iters start-vectors] :as kwargs}]
-  ;(let [[row-cnt col-cnt] (dim data)]
-    ;(case [(> row-cnt 1) (> col-cnt 1)]
-      ;[true true] (apply powerit-pca data n-comps kwargs)
+(defn wrapped-pca [data n-comps & {:keys [iters start-vectors] :as kwargs}]
+  ; gracefully handle all of the weird cases
+  (match (map (partial dimension-count data) [0 1])
+    [1 n-cols]
+      {:center (matrix (repeatv n-comps 0))
+       :comps  (into [(normalise (get-row data 0))]
+                 (repeat (dec n-comps) (repeatv n-cols 0)))}
+    [n-rows 1]
+      {:center (matrix [0])
+       :comps  (matrix [1])}
+    :else (apply powerit-pca data n-comps kwargs)))
 
 
-(defn pca-project [data pcs]
+(defn pca-project [data {:keys [comps center]}]
   "Apply the principal component projection specified by pcs to the data"
   ; Here we map each row of data to it's projection
-  (mmul data (transpose pcs)))
+  ; XXX - still need to verify this...
+  (mmul (- data center) (transpose comps)))
 
 

(ns polismath.pca
  (:refer-clojure :exclude [* - + == /])
  (:use polismath.utils
        clojure.core.matrix 
        clojure.core.matrix.stats
        clojure.core.matrix.operators))

(set-current-implementation :vectorz)


(set! *unchecked-math* true)


  iters iterations and starting vector start-vector (defaulting to 100 and 111111 resp)."
  ; need to clean up some of these variables names to be more descriptive
  (let [iters (or iters 100)
        n-cols (dimension-count data 1)
        start-vector (or start-vector (repeatv n-cols 1))
        ; XXX - this add extra cols to the start vector if we have new comments... should test
        start-vector (matrix
