mainloop improvement