Fix crash on exit

Static destructors were being called in an inconvenient order, replace that with a pointer and check for nullity