Modularized script into logical functions