@SᴀᴍOnᴇᴌᴀ The meta question you pointed to for
this refactoring question does apply, the OP specifically stated he wanted to refactor it with functions. You might note that I didn't vote to close when I posted my comment because I wanted to wait and see if the OP would change the question to a review. I might VTC tomorrow. Do you agree with what I'm saying?