refact and fix some bugs