Fixed bugs in error condition checking