is there a way to make this whole try block some function, instead of copy/pasting it tens of times?