Added some unit tests, identified some issues and fixed them