move js code to a closure