Add error handling

Fixes #4.
Adds exception handling to the main loop.