Fixed a bug in error handling