refactor logic into models