Restructured the apply method to be more modular.