How would you approach refactoring a legacy codebase while ensuring that existing production features remain stable?