Fixed scope issues with apply function

Wasn't accessing functions correctly and was getting reference errors.