Code dedoublication in models/models.go

Just some code dedoublication in models/models.go

The diff is a littlebit strange. I just pull out the doubled code in a new function and made one minor structural change.