moved all the $scope altering code to the top of the file