Move some of the new code into a separate function