Describe a time you had to refactor a legacy codebase. What was your strategy for ensuring stability?