Israeli Prime Minister Benjamin Netanyahu on Wednesday completed the formation of a new coalition government, reaching a last-minute deal with a nationalist party just before a midnight deadline.

The late-night deal saved Netanyahu from the unthinkable scenario of being forced from office. But it set the stage for the formation of a narrow coalition dominated by hard-line and religious parties that appears to be on a collision course with the U.S. and other allies.

With a slim majority of just 61 seats in the 120-seat parliament, Netanyahu could also struggle to press forward with a domestic agenda.

After Netanyahu's Likud Party won March 17 elections with 30 seats, it seemed he would have a relatively easy time forming a coalition. But during a six-week negotiating process, the task turned out to be much more difficult than anticipated, as rival coalition partners and members of the Likud jockeyed for influential Cabinet ministries.

The talks stalled this week when Foreign Minister Avigdor Lieberman, a longtime partner of Netanyahu's, unexpectedly stepped down and announced his secular nationalist Yisrael Beitenu party was joining the opposition.

That left Netanyahu dependent on Jewish Home leader Naftali Bennett, a former aide who has a rocky relationship with Netanyahu. The talks with Bennett stretched throughout the day and well into the night before Netanyahu called President Reuven Rivlin, as required by law, to announce the deal.

Netanyahu had until midnight to speak to Rivlin. Otherwise, the president would have been required to ask another politician to try to form a government.

The Jewish Home party is linked to the West Bank settler movement. It opposes peace moves toward the Palestinians and has pushed for increased settlement construction on occupied lands — a policy that is opposed by the U.S. and European countries.

His other partners include Kulanu, a centrist party focused on economic issues, and two ultra-Orthodox religious parties.