Refactor this to make it easier to understand, less bug prone, more performant, and adhere to best practices. ```python async def _get_thread_as_script(self, context: BoltContext, ts, channel_id): retval = "" replies = await context.client.conversations_replies( channel=channel_id, ts=ts, inclusive=True, limit=100, token=context.user_token, ) users = {r["user"] for r in replies["messages"] if "user" in r} reaction_users = { r2 for r in replies["messages"] if "reactions" in r for reaction in r["reactions"] for r2 in reaction["users"] } users = users.union(reaction_users) usernames = await self.user_cache.get_usernames(context, users) for r in replies["messages"]: username = usernames[r["user"]] retval += f"*{username}*: {r['text']}\n\n" if "reactions" in r: retval += await self._get_reactions_as_text(context, r["reactions"]) return retval ```