Moved some of the vaguely-scoped main code into a main block