Refactor into functions