Minor improvements

- Combine some vars into single ones
- Remove unused firstRow in sortTable
- Remove `k` variable in sortTable, and use `i` instead