Split code out
Theta function