fixed valgrind errors for exiting the program