unit test and bug fixes