Bugfixes for error handling