Added code to automate running multiple trials and averaging results.