Fix a memory leak due to linked lists not being deallocated properly.