added a few null pointer checks