now with sorting and better performance