fix bugs and add comments