Abstracted code into function