Refactored functions to make code more readable by making them smaller