This is a long-ish function. Possibly make this setup loop its own routine?