Fix more bugs (mostly possible NULL pointer dereferences)