add some comments and refine the code