Improve and abstract model code