update variable only when necessary