Fix for weird behavior of pointers. Possible memory leak?