Now that this has an answer posted, I don't agree that it is not too broad. Nevertheless, it is kind of turning the standard Q&A model on its head, in that what you are actually asking is "please suggest improvements to this answer", not "please answer this question". If what you are asking is "how can I improve this code", I would recommend aggregating the question and answer into one complete, working, snippet (as opposed to scattered code blocks), and post that as a question on
Code Review. This is kind of unclear as a StackOverflow question right now. —
pppery 39 secs ago