speedup texture sort function and some cosmetics