Add comments and reorganize the code.