trimmed and debugged tablesort functionality