Refactor to separate function