Writing the reducer