Corrected NPE
Performed some code cleanup and removal of dead code