implemented P2 and small improvement for P1