fixed the bugs in error handling