Refactoring of loopmodel_template