refactored more code from the view to the model