Made th code more dynamic and put more of the functionality into the controller rather than the views