Replace context manager with atexit handler