fix some bugs, add more test and coverage