Moved most of the I/O to a new setup() function, so the main method is cleaner.