Made some improvements to the error handling