Add an option to register atexit functions, first step to implement #281