Extract code to a function