Added some null checks that cause NPEs