Moved logic from index view into the controller