php math fix and distance tracking