Fix crash on shutdown