Fix NPE
Fix compile error in tests
Fix IllegalReferenceCountException
More fixes