Implemented reducer function