Unit test implementation and bug fixes