Fixed problem with duplicate freeing the memory in some cases