A deal in principle to end the bitter stalemate over milk prices has been hailed as a "good step forward" by the Scottish Rural Affairs Secretary.
Richard Lochhead stressed the dairy sector was "not out of the woods yet", however.
Mr Lochhead spoke after crucial talks between dairy farmers and processing firms resulted in a deal in principle being struck.
The talks, which were held at the Royal Welsh Show in Powys, took place after a tense few days for the industry, which involved several dairy factories being blockaded.
Several agricultural unions say their members are angry at being paid less for the milk they produce by big supermarkets — which aim to keep the cost of dairy products for customers as low as possible.
Following hours of talks, both sides have agreed to sign up to a new voluntary code of practice.
It means firms buying milk, such as big supermarkets, would give a "sensible" notice period when changing their prices — so farmers would have enough time to opt out of any deals.
Mr Lochhead said: "Today's agreement is a good step forward which addresses some of the most important issues, but our dairy sector is not out of the woods just yet.
"The test of success for our farmers will be when we see them being paid a decent return — and certainly one that is above the cost of production.
"There is still much work to do and we will continue with our plans to consult on legislation, the opportunity for which stems from the recent EU dairy package."
UK Government officials have said they could still bring in legislation at a later date if the code failed to work but admitted ministers would still not be able to dictate prices.
Mr Lochhead and Alun Davies, the Welsh deputy minister for agriculture, met Scottish and Welsh farming groups on Monday afternoon to discuss the deal.
Prior to the agreement on the new code of practice being reached, Mr Lochhead told BBC Radio Scotland that a "robust" code of practice was needed to help "rebalance what is a very unbalanced supply chain at the moment where the dairy farmer is getting a very raw deal".
He added that dairy farmers "are getting the rawest deal of everyone in the supply chain" and "are being squeezed by the competition between the retailers and milk processors".
Labour rural affairs spokeswoman Claudia Beamish welcomed the agreement on the terms of an industry code of practice, saying this would "lay the foundations of a new deal between farmers and retailers".
Ms Beamish said: "For too long dairy farmers have put up with wholly unbalanced terms and have been struggling to cope in an increasingly unworkable financial situation.
"It cannot be right that supermarkets use milk as a loss leader whilst farmers are paid less for the milk than it costs them to produce it. This code will go some way to redressing that balance."
But while the Labour MSP said this was a "good start" she stressed: "The Scottish Government must be prepared to introduce legislation to the Scottish Parliament should this voluntary code of practice fail.
"Labour in Wales has already begun drafting such legislation and the SNP must do the same to ensure Scottish dairy farmers are given a fair price and fair contracts."
Scottish Secretary Michael Moore said: "This agreement is good news and a solid foundation on which to build a system fairer for farmers and better for the consumer. Creating more flexibility in the commercial relationship between milk producers, processing firms and supermarkets is a significant step in the right direction.
"The UK Government is committed to that process and will continue to work across the industry to support our rural communities, and with retailers, to help secure fair and sustainable prices for Scottish milk."
Scottish Conservative rural affairs spokesman Alex Fergusson MSP said: "Today's meeting has clearly made progress, with an agreement to hold further talks on a code of conduct next month and, crucially, a further agreement to legislate on this issue if agreement cannot be reached voluntarily.
"It therefore makes total sense that the major retailers and processors reverse the cuts in milk price which they have announced for August, and I completely endorse the calls for this price cut reversal that have been made.
"The price has already been cut — by two pence per litre in June — and that should be all any producer has to bear until talks are concluded."