Explain how you would refactor a poorly structured function to improve its readability and testability.