replaced O(n^2) sort with sort.Sort