add comments and refactor the code