Fixed a memory leak crash error and added some global variables