merged mainloop