Comment and format main loop logic to be readable