make sorting work, still need the optimized version