add comments and optimize