rewrote sort function to use less processing