Fix bug, add $max_int, and change sort() to ksort() -refs BT#8185