refactored reduce.hpp