Restructured the apply method to be more modular.
Merge origin/master