fixes for key not found and null reference exceptions

Good catch nate, I've actually got the same change sitting here that I hadn't got around to pushing to you over the last few weeks !