Put main process in a loop; fix ordering issue in index.js