Smaller improved sort funtion