Pgm p2 broken

Fix for issue #811 and a small refactorisations, tests.