add comments and fix some bugs