bug fixes and unit test