Added PAM algorithm: need to fix occasional loops