Refactored code in smaller, more readable functions.