Fixed some scoping issues with apply calls