further work on reduce part
fixed reduce function