refactored controller and helper logic to models