Fix bugs related to handling TID responses. Fix a few memory leaks.