United Parcel Service Inc. rode higher prices and strong delivery demand driven by e-commerce to post profit that topped analysts’ expectations. It also raised its operating margin outlook to 13% for 2021 from an earlier target of 12.7%.

UPS and rival FedEx Corp. have been grappling with hefty volume since the pandemic hit last year. Both have aggressively raised prices to offset the expense of handling more residential deliveries, which have grown faster than more-profitable commercial packages.

The price increases and shift to higher-profit deliveries drove a 13% gain for UPS’s overall revenue per package, offsetting a 2% drop in average volume. The drop in delivery volume was led lower by a 2.7% decline for U.S. domestic, the company’s largest unit, and follows a surge in demand a year earlier.

Commercial packages are making a comeback as the economy recovers from Covid-19 lockdowns and as UPS shies away from the least profitable of those residential deliveries, also known as B2C, or business-to-consumer.

“UPS has been growing its high-margin businesses like health care, pharmaceutical and medical devices while lessening its dependence on the lower margin B2C business,” said Helane Becker, an analyst with Cowen Inc., in a note to clients.

Adjusted earnings hit $2.71 a share in the third quarter, up from $2.28 a year earlier, the Atlanta-based courier said Tuesday in a statement. Analysts had predicted $2.54. Revenue rose 9.2% to $23.2 billion in the third quarter, while analysts had expected $22.6 billion.

The company has weathered a labor shortage better than its key competitor because, unlike FedEx, it has a union workforce and pays the highest wages in the industry. Compensation and benefits rose only 0.6% in the quarter from a year ago. Still, UPS’s costs for purchased transportation climbed 18% as the courier turned more to outside companies to move packages. Fuel costs jumped 54% on rising gasoline prices.

UPS shares rose 5.7% in early trading in New York. The shares have gained 21% this year through Monday, just shy of a 22% increase for the Standard & Poor’s 500 index.

The courier has focused on raising profit margins under Carol Tome, who took over as chief executive officer last year. Tome laid out a strategy of “better, not bigger” to rein in UPS’s past tendency to gobble up all the volume it could no matter how well it paid. She also sold the lower-margin freight business in April for $800 million and announced in September an agreement to purchase Roadie, a same-day delivery startup.

The company on Tuesday also increased capital spending plans for the year to $4.2 billion from $4 billion.

UPS’s adjusted operating margins rose to about 12.8% from 11.3% a year earlier as price hikes and its focus on higher-value packages made up for rising costs to hire workers and protect them from Covid-19. Analysts had estimated margins of 12.2%.