I have reached to what I think of as an optimised way of doing the same. This code just do not create a new variable, but uses the function in order to shorten the code and make it more readable. I hope you find it well and that this will help you.
line 15 inside the main function