Two years ago, just after Gov. Martin O'Malley won his second term, Maryland's Spending Affordability Committee got a sobering briefing from state fiscal analysts. Thanks to federal stimulus funds, Maryland had been scraping by despite the deepest recession in decades, the collapse of the housing market, and a disappointingly slow start to the state's slots program. But that money was about to run out. The next budget year promised a $1.6 billion shortfall, and things were only expected to get worse from there.

This week, the same fiscal analysts gave their briefing to the Spending Affordability Committee, and things look radically different. The state goes into preparations for next year's budget with a trifling $27 million shortfall. That's an improvement of more than $1.8 billion over what that 2010 report predicted. Some of the improvement is the result of tax increases and a slightly improving economy. Part of it is the result of cost shifts to local governments and the use of borrowing to paper over operating budget shortfalls. But the untold story of Governor O'Malley's second term is a highly successful partnership between his administration and the legislature to constrain costs. The state's fiscal picture isn't perfect, but the progress made in the last two years represents a tremendous accomplishment.

Maryland's constitution, like that of virtually every state, requires that the budget be balanced every year, but there is a difference between meeting the letter of the law and achieving true sustainability. In late 2010, legislators and the administration set a goal for themselves: Not only would they find a way to balance the books — whether through transfers, fund raids or whatever gimmicks they could find — but they would also embark on a three-year effort to achieve a true, sustainable "structural balance." They are tantalizingly close to achieving it.

During this term, Governor O'Malley and the General Assembly have enacted two tax increases — a 2011 increase in alcohol taxes, the first such increase in decades; and a 2012 increase in income taxes on those in the top 20 percent of Maryland earners. Current projections call for those two increases to produce about $325 million next year. The shift of some teacher pension costs to the counties and Baltimore City, coupled with an increase in local aid to some jurisdictions, nets out to a $150 million improvement in the budget picture for next year. Another $230 million comes from the O'Malley administration's practice of using bond funds to cover costs like Program Open Space that can be construed as capital projects. The revenues that would otherwise have gone to cover those programs are then spent on other operating expenses. And thanks to somewhat better than expected budget news in the last few years, the state has a $482 million fund balance it did not expect.

The expansion of Maryland's gambling program, authorized in an August special session of the legislature and ratified by voters this month, will add about $60 million to next year's budget — yet it is still not part of the explanation for how the state's budget shortfall has shrunk so much since 2010. The reason is that even with the expansion, projected gambling revenues for next year are still below what analysts expected them to be two years ago, a reflection of delays in the roll-out of Maryland's gambling program and increased competition from surrounding states.

The bottom line is that the single biggest thing the state has done to improve next year's budget picture is to constrain costs. Maryland's expected operating expenses in the fiscal year that starts next July total about $16.1 billion, more than $1.1 billion less than analysts forecast they would be just two years ago. The O'Malley administration has found ways to significantly reduce costs to the Medicaid program without limiting eligibility, and it has achieved hundreds of millions in savings by reducing the number of state government employees and limiting salary growth. Meanwhile, lawmakers have also taken steps to shore up the state employee pension system and to reduce unfunded liabilities for retiree health care.

Maryland is not totally out of the woods. The $27 million figure still relies on some one-time money and fund transfers. But as things stand now, Maryland's structural deficit is just $417 million, less than a quarter of what it would have been if the governor and legislature had not taken the steps they have during the last two years. There will still need to be substantial spending cuts in the budget proposal Governor O'Malley submits to the legislature in January. And if President Barack Obama and congressional Republicans fail to come to a sensible compromise to avoid the fiscal cliff, Maryland's budget could be in for another massive hit. The governor faces a tricky decision to determine how much money to set aside to prepare for that contingency.

Meanwhile, he is likely to face pressure from assorted interest groups to begin to restore funding for programs that have suffered cuts in recent years. It's not time for that yet. But we are on the verge of having a conversation about what is the most prudent way to invest in Maryland's future rather than what is the least painful way to cut.