Refactor inner functions