Few companies have matched Amazon's (AMZN -2.70%) stock performance over the past decade, rising more than 650%. However, it hasn't performed well lately, with the stock down 46% from its all-time high set in 2020.

So are Amazon's best days behind it? Or has the recent weakness set the stage for a strong recovery?

Amazon is producing cash again

When the pandemic began, it was obvious that Amazon would benefit from the ensuing e-commerce rush. However, those e-commerce gains weren't permanent, and many investors reverted to their regular shopping habits when the world opened back up.

Unfortunately, Amazon had already invested heavily in warehouses and personnel to fulfill the demand. This caused Amazon to become free-cash-flow (FCF) negative, with the company rapidly burning through cash. However, after warehouse closures, layoffs, and the elimination of some programs, Amazon has returned to a cash-generative state.

AMZN Free Cash Flow Chart

AMZN Free Cash Flow data by YCharts

With nearly record FCF generation in the fourth quarter, Amazon's turnaround is almost complete. This is excellent news for investors, as Amazon can utilize those cash flows to buy back stock or make other investments.

Among the segments that Amazon could invest in are cloud computing and advertising, which have immense potential.

Amazon's non-commerce segments are doing well

According to Synergy Research Group, Amazon Web Services (AWS) is the industry leader in cloud computing, with an estimated 34% market share. What's even more impressive is that this industry is expected to grow to $1.6 trillion by 2030, compared to a $380 billion market in 2021. With AWS generating $80 billion in sales throughout 2022 and turning 29% into operating income, this segment is a true cash cow with massive upside.

A crucial emerging segment for Amazon is its advertising division, which grew at a 19% pace in Q4. What makes this division special is that practically all advertising companies saw their revenue shrink in 2022 thanks to a weak economic outlook. With Amazon's revenue steadily up in 2022, this segment is one to watch, especially since it was the fourth-largest division behind online stores, third-party seller services, and AWS.

Amazon has shown its ability to create multiple segments that increase its revenue tremendously; now, the key is turning that revenue into profits. With Amazon's various cost-cutting measures, these should be just around the corner.

Because Amazon has been inconsistent with its profitability levels, I'll use the price-to-sales (P/S) ratio to value the stock.

AMZN PS Ratio Chart

AMZN PS Ratio data by YCharts

Despite Amazon creating multiple new high-margin segments (like AWS and advertising), the stock still trades like it's solely a commerce company. This seems like an error, and investors should take any chance they get to open or add to an Amazon position, as the stock isn't richly valued.

2023 will be another exciting year for Amazon, and investors must dig through its first-quarter report to understand how the cost-cutting measures are going. Additionally, if Amazon can keep growing its cloud computing and advertising services, it should be a boost to Amazon's profit margins, which could cause the stock to be drastically undervalued. I'm a buyer of Amazon's stock here, as I think it has strong potential to outperform the market over the next three to five years.