Move UI code into anonymous function