Add comments and remove duplicate code.