refactored code to put logic in controller for nested jobs