more cleanup and NPE handling