This bit seems copied from somewhere else, if so: please extract and make it into a utility function