this code repeats over and over. can we abstract it on a function?