Tesla surprised Wall Street Wednesday by posting a profit of nearly $22 million for the third quarter. It's a surprise because it's only the second time in the company's history that it has posted a quarterly profit.

Tesla attributes its newfound profitability to new product launches, increased store efficiency and new store openings. At the same time the company says its investment in self-driving hardware and other product enhancements position Tesla to gain market share.

"We've got a chance of being profitable," said Tesla's CEO Elon Musk about next quarter on the company's earnings call. The last time Tesla posted a profit was in the first quarter of 2013.

"That remains to be seen," says Michelle Krebs, senior analyst for Autotrader, about Tesla's future profitability. Krebs says the company was able to keep costs in check while focusing on meeting production targets. She adds: "Finally, Tesla has achieved a milestone that investors long awaited — a profitable quarter and a surprise one at that."

The profitable quarter comes at a particularly good time for Tesla, according to Krebs. Next month, the Tesla board votes on the controversial merger with SolarCity Corp. Musk is the chairman of Solar City, the maker and installer of solar panels, a company founded by his cousins.

In addition to the merger, Tesla is building its "gigafactory" which would be the largest battery factory in the world. The company sees energy storage and production as a key to growth as outlined in a letter to investors.

"Our energy storage products are gaining increased market acceptance, firmly establishing Tesla as a leader in energy storage solutions, and surpassing our competitors in the breadth and scope of our offerings across residential, commercial, and utility-scale storage markets. At the same time, we continue to lay the foundation for future growth. Gigafactory construction and Model 3 development both remain on plan to support volume Model 3 production and deliveries in the second half of 2017."

Tesla says its activity in the battery realm didn't keep it from achieving its internal goals. Musk told reporters the company is on track to build the Tesla Model 3, an affordable electric car. The company has set a goal of selling 500,000 vehicles by 2018. Currently, according to Musk, Tesla is building 2,000 vehicles a week. Tesla said earlier this month that it delivered 24,500 vehicles, a 70 percent increase from the same time last year. Tim Higgins of the Wall Street Journal reports on CEO's plan for expansion.

"Tesla needs about $2.5 billion through the end of 2017 for the Model 3 roll out and the completion of a huge battery factory in Nevada, according to Brian Johnson, an automotive analyst at Barclays."The improved results also could help Mr. Musk make the case that he can handle merging Tesla with SolarCity Corp., which could require additional cash. The combined companies ultimately may need to raise $12.5 billion for spending through 2018, according to Oppenheimer & Co. Tesla and SolarCity shareholders are scheduled to vote on a merger Nov. 17."

Copyright 2016 NPR. To see more, visit http://www.npr.org/.