Cleanup and fix NPE