moved repetitive code to function