Cleaned up sorted() as per Damien's suggestions.