abstracted a few code blocks into seperate functions