Move code into model