Improve TCP cleanup code