Fixed bug in handling error conditions