Lots of repeated code here. Worth an internal utility function?