add comments and refactor