Fixes and added unit tests