Fixing some NPE and similar small issues.