Add comments and refactor some code.