PAM algorythm - tweaking general algorithm