optimized sorting and run functions