Refactored logic back into controller