Cleaing up a lot of the code, adding comments, a few new unit tests