refactored repetitive code into functions