Crash fixes

Fixes a couple crashes which have happened, and crashes that could happen. Sorry about the formatting changes, my IDE does this sometimes. However it also automatically indents all my code and can automatically reformat code to be indented properly... so its a tradeoff.