Added some fixes and unit tests