Refactor function handling