move much logic into models