Added new unit tests and fixed some code accordingly