added a couple of null checks