Refactor into helper funcion