Robust CLI error handling

Fixes #528, fixes #524, fixes #518