Include some more checks and printouts to avoid crashing.