Move event listener initialization into a function so it can be called after widgets are initialized