restored rentrant error handling code from b153bfa