improve reduce code