Implements main loop and timer