simplified sort function, other small changes