Add more robust error logging