Moved all the model related code to a separate file and cleaned up some things.