Algorithm for PI