Improve code making use of lexical scope