And it was at that time that Avraham traveled from Elonei Mamre and went to the land of the Philistines and sojourned in Gerar. Twenty-five years after Avraham's dwelling in the land of Canaan, which is one hundred years of Avraham's life [(this is Year 2048)], Avraham came to the land of the Philistines, to Gerar, etc. And Avimelech sent and took Sarah, etc. And it was toward evening that he sat on the throne and fell asleep, and behold an angel of HaShem came with his sword drawn and sought to kill him for the woman, etc.
And on that night all the inhabitants of the land saw the form of a man with a sword and he struck all the inhabitants of the land [(Sefer HaYashar, chapter 21). Avimelech's rectification was through Avimelech son of Yeruba'al (see Year 2916)].