add comments and some minor bug fix.