Changed on_exit to atexit bacause on_exit is not POSIX