Refactored some controller code to belong in the model