script for PHrange optimisation (correct)