Refactored controller and js with a more readable implementation