>Also, there is enough code here where it might make sense to put it in a separate function.
Can you show me what you're thinking?