error handling code improved