use min/max functions instead of if statements