Refactured my code and created a new function