Improved logging of errors