Refactor this legacy code without changing behavior. First identify responsibilities, duplicated logic, hidden dependencies, and risky areas. Then refactor in small safe steps. Preserve public APIs unless a change is explicitly required.
Paste into your AI coding agent before starting a task to guide a multi-step workflow.