fixing bugs in error handling