Optimize sorting. Closes #60.