update the codes by adding comments and clean up
some errors