added mehdi's chainsort fixes