Add comments and refine the code.