Refactored some code, added a model factory and atom factory. All the different factories are a bit confusing; I should find a better way.