Better cleanup of allocated memory at exit
Using valgrind all the leaks were tracked and fixed.