NPE fix plus a bunch of dead code removed