added a couple of NPE checks