add test and fix some bugs