added some error handling code