refactored some code, used .apply the correct way