Move some code into a helper function. NFC.