Move some repeated code to utility function