NPE Fix Feature Branch

NPE fixes from rSmart/issues#243
This doesn't seem to include the commit I made. I amended ed1dc91 which doesn't seem to be included here.