Updated the sort algorythm to account for rows