added reduce function to main.c