Wrapped model code into function for x2 speedup

http://stackoverflow.com/questions/11241523/why-does-python-code-run-faster-in-a-function