Rearrange main() function and loop.