factored out mainloop as a module. removed duplication from mouse and touch event handlers in mainloop update