polish codes, fix typos and add comments