More changes

Replaced i=$[${i}+1] with let i++ -- seems to be fine
Fixed a couple of inconsistent variable names and bugs
Replaced some for loops with C like loops instead
sort[] is currently broken