this code is copy-pasted elsewhere and could be abstracted to a function