Clean up legacy or messy functions.
Refactor the following Python function to improve readability, structure, and performance. Use idiomatic constructs.