fixed error handling bug