Moved all non-rendering stuff out of main to various functions