The S&P 500 climbed 0.7% after veering between gains and losses through the day. The Dow Jones Industrial Average added 276 points, or 0.5%, and the Nasdaq composite rallied 1% after briefly losing all of an early 1.3% jump.

It’s a fitting finish to July for the U.S. stock market, which lurched up and down as oil prices shot higher because of the war with Iran and worries grew about whether Big Tech’s massive investments in artificial-intelligence technology will translate into profits and whether chipmaker stocks soared too high in the euphoria around AI.

Friday’s gains sent the S&P 500 to its first winning week in three, but the main measure of the U.S. stock market nevertheless finished the month with a tiny loss.

Amazon led the market with a leap of 15.3% after reporting much stronger profit for the latest quarter than analysts expected. Its profit more than tripled from a year earlier, thanks in part to an acceleration of growth in its cloud computing business.

Analysts said that could be a signal Amazon’s huge AI investments are paying off, and Amazon increased its forecast for how much it will spend on investments this year.