copied reduce code from the separate branch to the main branch