Cleanup local usage and add null check

http://openeye.openmods.info/crashes/e13ceca5ef849cfb091e5b77d462a5dd
Merge pull request #347 from Phyn/master

Cleanup local usage and add null check