Main loop scaffolding.