Extracted code to a function