Refactor global state into runtime