Move timer code to Mainloop()