fix min/max to prevent segfault when max<min