Move variable reset under if condition