Fix to avoid memory leaks