re-implemented min and maxby