ref #8222 Fix potential NPE + cleanup