Simplify error handling and improve sorting.

Closes #62