Refactor logic from controller into view helper