Fixing memory leak from linked list