Added a bunch of null protection