Refactor functions