Added some basic sanity check unit testing, and fixed some broken things that it exposed. Will be adding more unit tests shortly.