All of this is duplicated code. Maybe make a function for it.