Added an atexit handler as a safety net