Refactored into functions