SSP-925 some defensive code around null pointer and other changes to
make the class more bullproof