turned repeating code into function