Fix crash on app termination