fix scope order of code