Fixed several memory leaks and memory clean up at exit; Updated scripts.