NPE fix and other fixes