Handled some memory leaks using a very naive solution.

Will work on it later to remove all the memory leaks