Refactor into function