ternary -> Math.min
Fixed sortedInsert call to maintain unique invariant