refactor code for model formula to improve portability