Removed qsort dependency since platform support is a mess.

Added insert sort implementation.
Also use TBTempBuffer for temp array to anchor its destruction.
Compilefix for some debug code.