Moved model code to the model