various optimsations to error handling