Move some code to a separate function