restructure scope control code