pulling loop body into named functions