Fixed weighting in MIN/MAX, added unit tests.