final version of code for model