fix error handling code