refactor function