Refactor out fetching and printing into functions