Port part of full model code to Python