Modifications to fix NPE and some unit tests.