Refactor to functions