hot fix GreatestNumberUsed for pg in while loop