debugged PH optimization and corresponding function in api.py