unit test and some fixes