Some fixes to avoid crashes.