modified KM algorithm