modified: dont use Math.min