made logging in more robust with better error handling