Fix minor bugs and add comments