added better error logging function