Moved block of code into a function