Bug fix in mainloop.