Islam was first brought to Algeria by the Umayyad dynasty following the invasion of Uqba ibn Nafi, in a drawn-out process of conquest and conversion stretching from 670 to 711. The native Berbers were rapidly converted in large numbers, although some Christian and probably pagan communities would remain at least until Almoravid times. However, as in the Middle East itself, they sought to combine their new Islam with resistance to the Caliphate's foreign rule - a niche which the Kharijite and Shiite "heresies" filled perfectly. By the late 8th century, most of Algeria was ruled by the Rustamids, who professed the strictly puritanical but politically moderate Ibadhi sect and saw the Caliphs as immoral usurpers. They were destroyed by the Shia Fatimids in 909, but their doctrine was re-established further south by refugees whose descendants would ultimately found the towns of the M'zab valley in the Algerian Sahara, where Ibadhism still dominates.