Move engine functions to class. Encapsulate context state in class.