split while(True) into setup(), loop(), main() functions