Cherry pick from f8c1c8f6a6: Moved most globals into struct.