Refactor setup and use atexit