Describe a time when you had to refactor a complex piece of legacy code. How did you approach it, and what was the outcome?