can we abstract this code in a function?