Tesla may have struggled through “production hell” to build the Model 3, but the car has helped the company set a new record: $4 billion in revenue in the second quarter of 2018, according to an earnings report published on Wednesday. It also reached another milestone: the Model 3 is finally starting to make Tesla some money. The plan to sell the more expensive versions of Tesla’s mass market car so that the company doesn’t “die” is working.

The Model 3’s gross margins, a measure of the revenue that the company retains after costs associated with producing it, turned “slightly positive” in Q2, according to the company. That’s despite the fact that production was still ramping up and that Tesla had yet to ship any of the expensive All-Wheel Drive performance versions of the car. “This was a significant achievement in the ramp of Model 3,” the company concludes in a letter to investors released with the report.

“This was a significant achievement in the ramp of Model 3.”

Until recently, Tesla had been losing money on every Model 3 that it shipped. Tesla says that the Model 3’s gross margin “should grow significantly” to approximately 15 percent in the third quarter and 20 percent in fourth quarter, thanks to “continued reduction in manufacturing costs and to some extent an improving mix.” From here on out, CEO Elon said during a call with analysts Wednesday evening, “the goal is to be profitable and cash flow positive every quarter going forward.”

Tesla says that it expects to make 50,000 to 55,000 Model 3 vehicles in Q3, which would represent an increase of 75 to 92 percent over its second quarter numbers. The company also says “deliveries should outpace production in Q3 as our delivery system stabilizes,” a hint that the many finished Model 3s currently sitting in parking lots are ready to move.

Tesla says it is continuing to produce “approximately” 5,000 Model 3s a week, a goal which it first achieved in late June. The company says it is still on track to hit 6,000 cars a week in late August, and it’s now aiming for a weekly production rate of 10,000 by early 2019. Cumulatively, Tesla made 28,578 Model 3s and delivered 18,449 of them in the second quarter alone.

The company’s trouble in ramping up production of the Model 3 was well-documented, something Tesla admitted in the opening line of the letter. “[N]o production ramp of any other product has been as closely watched and debated as that of Model 3,” Musk and CFO Deepak Ahuja write. In order to hit that oft-delayed target of 5,000 Model 3s per week, Tesla fired up a new production line in a tent outside its Fremont, California factory in June, reportedly borrowed workers from other parts of the company, and churned out Model 3s 24/7.

Tesla’s search for overall profitability continues, as it reported a loss of $743 million for the quarter — its third biggest quarterly loss ever. (The company reported losses of $770 million and $785 million in the previous two quarters.) But, buffeted by Model 3 sales, Tesla grew its revenue for the sixth consecutive quarter despite a slight drop in the money coming in from its energy generation and storage business. It also emerged with $2.2 billion in cash and cash equivalents, which should help Musk continue to make the case that the company won’t need to raise new funding in the coming quarter.

That said, there are big projects ahead for Tesla that will require far more capital than it currently has on hand. The company is planning at least two new cars — the Model Y and the second-generation Roadster — and an all-electric semi truck. Musk also struck a deal in July to build a third Gigafactory in China. In the letter released on Wednesday, Tesla said production in the China factory is slated to start in three years, not two as originally stated. The company is also reportedly in talks to build a fourth Gigafactory in Europe.

Some analysts expect those factories to cost as much, if not more than the $5 billion price tag for the first Gigafactory in Nevada. But on the earnings call, Musk said he’s confident Tesla could build a China Gigafactory for “a lot less” than that. “Just a guess,” Musk said, but the cost might be closer to $2 billion. As for where that money will come from, Musk said the company will likely raise debt financing locally in China.

Regardless of how some of these initiatives pan out, Musk and company sounded happy to look forward on the analyst call. Because beyond the hellish ramp up of the Model 3, it’s been a manic last few months.

Starting with the previous quarterly earnings call, which happened in the middle of what seemed like the darkest days of the Model 3 ramp, Musk blew up at analysts for asking “boring, boneheaded” questions and instead turned to a YouTuber for a large portion of the call. Two weeks later, a report surfaced that Tesla had opted against including driver monitoring technology in its cars, which could have prevented some of the more high-profile accidents involving Autopilot.

It’s been a really wild summer for Tesla and Musk

Then, Consumer Reports discovered the Model 3’s braking distance was worse than that of a Ford F-150. (Tesla rushed a software update that fixed the issue.) In June, Tesla laid of thousands of workers, and Musk took issue with the “Russian nesting doll” of contractors that the company relied on. Days later, Musk warned his company of alleged “sabotage” from within its ranks, and shortly after that filed a lawsuit accusing former Gigafactory employee Martin Tripp for hacking and disseminating trade secrets.

Then there was the tent, the trade war, and the news that the Model 3’s eligibility for the federal EV tax credit is officially winding down. One of the company’s top engineers left, and Tripp hit back with a counterclaim of his own. And there was an entire saga around Musk’s attempt to develop a solution to help rescue a soccer team trapped in a cave in Thailand, which resulted in Musk calling one of the divers a “pedo” (and eventually walking the claim back).

On the call Wednesday, Musk sounded exhausted. The past few months had “burned a lot of neurons,” he said. “[The] mental scar tissue is, like, next level.” But he apologized to analysts for his actions on the last earnings call, and brought in more than half a dozen other Tesla executives and high-ups so he could talk about other efforts he felt had maybe recently gotten lost in the mix. “Really never been more excited about the future of Tesla,” he said.