Fixing various crash bugs