restructure engine code