Added UnitTests and fixed minor issues

Added unit tests to replace the previous LegacyTests (though I have
preserved the option to run those old tests as well).