A bunch of unit tests

And some minor restructuring to support the tests