Move functions to where they're used