Tick events, move some functions to util.main