Refactoring logic into model.