Brexit: What happens now?
A new version of the Brexit deal has been agreed between the EU and UK.
On 19 October, for the first time this century, MPs will sit in the House of Commons on a Saturday. They will be given the chance to vote on the deal.
As things stand, though, Northern Ireland's DUP - which has 10 MPs - does not support the deal so the vote could be close.
The new deal does not involve the Northern Ireland backstop. That was the measure from the original withdrawal agreement aimed at preventing any possibility of border posts and checks between Northern Ireland and the Republic of Ireland.
In the new version of the deal Northern Ireland would continue to follow EU rules and standards on all goods. That would remove the need for regulatory checks on goods crossing the Irish border.
In addition, Northern Ireland would leave the EU customs union but EU customs procedures would still apply on goods coming into Northern Ireland from Great Britain.
The Northern Ireland Assembly would have the power to cancel these special arrangements after four years. But that would require a majority of assembly members to agree. And if the Assembly didn't vote, the arrangements would continue.
There is also a new political declaration. That's the non-binding part of the deal which sets out plans for the long-term future relationship between the UK and the EU.
If MPs reject the deal, then under the so-called Benn Act the prime minister is obliged to request an extension of Article 50, delaying Brexit.
Cabinet ministers have said the government will obey the law, but the prime minister has said he will not ask for an extension. There has been some speculation about finding a loophole in the law or using some other device to avoid Mr Johnson himself having to ask for the extension.
For example, it's been suggested that he could request an extension but at the same time tell the EU to ignore his request.
If Mr Johnson refuses to write the letter asking for an extension then it's likely he will face a legal challenge. The Scottish Court of Session could rule as early as 21 October.
No-deal Brexit on 31 October
The default position is still that the UK will leave the EU on 31 October at 23:00 GMT.
Even if the prime minister requests an extension there is no absolute guarantee that the other EU countries would agree.
Leaving without a deal (or withdrawal agreement) means the UK would immediately exit the customs union and single market - arrangements designed to make trade easier.
Many politicians and businesses say this would damage the economy. Others say the risks are exaggerated.
An early election is widely expected after 31 October when Brexit is currently scheduled to happen. It's unclear, though, whether that would be later this year or early next year.
If a Brexit is delayed, the House of Commons might be asked again by the government to back an early general election. That requires a 2/3 majority in the House of Commons and so far MPs have been unprepared to agree.
An alternative route for the government would be a short new law specifying the date of an early general election - this would require only a simple majority and not need two-thirds of MPs.
There is another much more dramatic way - the prime minister could call a vote of no confidence in his own government.
Vote of no confidence
At any point the opposition could call a vote of no confidence in the government. Labour leader Jeremy Corbyn has previously said he would table such a motion.
If more MPs vote for the no-confidence motion than against it, there would then be a 14-day window to see if the current government - or an alternative one with a new prime minister - could win a vote of confidence.
If no-one does then a general election would follow.
There could also be another referendum although it would certainly require a Brexit delay and, most likely, a change of government first.
The referendum could have the same legal status as the one in 2016. It would be advisory, and the government would have to decide how to respond once the result was known.
An alternative would be to hold a so-called "confirmatory" referendum. That would be between a particular Brexit deal and remain - or possibly with no deal as an option. The result of this kind of referendum would be legally binding.
Either way, the new referendum would require legislation to be held. There would also have to be time for the Electoral Commission to consider the question wording - especially if it's a referendum with more than two options.
Experts at the Constitution Unit at University College London say it would take a minimum of 22 weeks.
There is also the legal option of cancelling Brexit altogether by revoking Article 50.
But clearly, this is not something the current government is contemplating - so it's only really possible to imagine this outcome after a change of government.
The Liberal Democrats have said that if they won a majority in the House of Commons they would revoke Article 50 and cancel Brexit.