Added logging and atexit function