Unrefined but working version of the automated script.