Fix reduce algorithm