Adding a few unit tests

and also fixing bugs found with them