updated sampling and topic reasoning code