CON-1709 | moved most of the logic from the template to the controller