some improvements to error handling