add better error management

Mostly in the form of exception handling