Algorithm KMM working