Rewritten logic to avoid memory leaks, but still not working, can't figure out why.