Can you factor the relevant parts of this function out into a `setup` function?