Fix 2 possible crash bugs found by testing-environment