Replace terrible ad hoc thing with reasonable formula.