Refactor profile handling into a new function.