Minor changes to fix a few bugs
Add a few unit tests