add comments and revised code