fix some bugs and add explanation to some function and rearrange form a little