Add some checks to prevent NPE