Refactor function