How an obscure company pivoted, then pivoted again, nearly ran out of money—and built a hit product.