Using http://lib.stat.cmu.edu/multi/pca.c as a starting point