Adding improved error logging