Refactor lambdas into named functions