Improved error handling

see #162