Add avoiding NPE and add logging