moving lock outside function