Cleaned the code up.
We're also now keeping a running maximum rather than re-evaluating all possible values once they've been computed.