Moved reference number generation into the model