reorganized script flow