Finished rewriting.

Much cleaner than it ever has been before, down to about 375 loc (w/
comments) as opposed to the 675 behemoth we had before.

Works pretty well, commit as V1.1