Refactored to smaller functions