Improved error logging