rewrite to avoid "global vars" NOTE