BlackBerry (BBRY) surprised Wall Street with a net profit in its fiscal first quarter, although sales continued to lag as the smartphone maker struggles to attract users.

BlackBerry said Thursday it earned $23 million, or four cents a share, compared with a loss of $84 million, or 16 cents a share, in the year-ago period. Excluding noncash income and restructuring charges, the company posted an adjusted loss of 11 cents a share.

Revenue dropped 69% to $966 million, with 39% of sales generated by hardware. Adjusted gross margin rose to 48% from 43%.

Analysts expected revenue of $963 million and a steeper adjusted loss of 26 cents a share.

Shares rallied 13.3% to $9.39 in recent trading. BlackBerry was already up 11.4% on the year through Wednesday’s close.

Chief executive John Chen, who took charge of the company in November, has led an effort to reduce costs and restructure operations.

The Ontario-based company ditched an effort to sell itself last year. In December, BlackBerry moved to outsource the production of its handsets to Foxconn, a Taiwanese firm that produces Apple’s (AAPL) iPhone and other popular devices.

On Thursday, Chen said BlackBerry remains on track to achieve hardware profitability sometime in fiscal 2016.

“Over the past six months, we have focused on improving efficiency in all aspects of our operations to drive cost reductions and margin improvement,” Chen said in a statement. “Looking forward, we are focusing on our growth plan to enable our return to profitability.”

BlackBerry saw improved demand for its latest devices in the first quarter. During a conference call with analysts, Chen noted that BlackBerry 10 devices accounted for 65% of hardware sales in the first quarter. In recent quarters, BlackBerry 7 outsold newer versions of the company’s smartphones.

The company’s cash and investments balance checked in at $3.1 billion at the end of the first quarter, up from $2.7 billion.

The BlackBerry 10 operating system is slated for an upgrade in October, Chen said. BlackBerry also plans to bring its messaging service to Windows devices in July, continuing its effort to widen BlackBerry Messenger’s footprint. Chen added that BBM could move toward a mobile payment service in the future.

Earlier this week, BlackBerry unveiled an agreement with Amazon.com (AMZN) to give BlackBerry smartphone users access to the e-commerce giant’s app store. The partnership will give BlackBerry another 240,000 apps once BlackBerry 10.3 is released in the fall.

Follow Matthew Rocco on Twitter @MatthewRocco