minor NPE fix, minor refactoring