Improve sort. Refactor memory management. Closes #65. Closes #63.