Proper exception handling

Fixes #2