in terms of breaking the 3 sections out into a function .. i'm not sure how to go about that .. can you give me more ?
A general "recipe" for making repeated code into a function is to make the common parts the body of the function, and make the parts that differ parameters of the function.