Fix the Unit tests and some small bugs in the code