Phreg 1312 rebased

rebased version of #1312

removed duplicate commits with interactive rebase dropping those commits

unit tests for test_phreg pass locally (py 2.7)