fix some bugs and add comments