Completed the implementation of the reduce part