fixed mainloop running multiple times