fix NPE
some more cleanups and fixes