This block of code is repeated in several parts of the code, do you think it could be made into a function?