fix some bugs and clean the code