Refactor legacy code for better clarity and maintainability