Fixed bugs in exception handling