optimizing code
running max_by instead of sorting