Improved error handling

More tests, particular for failure testing