cleaned up error handling code