Discuss how you would refactor a piece of legacy code to enhance its readability and maintainability.