Implement main loop