Mainloop: avoid calculating timeout every time