Move some logic into functions.