Add null checks to avoid NPE