Fixed bugs and unit test