extracting code to a model method

This extracts some code to a model method for easier re-use.