Refactored code to stop code repitition