moved herems to closure to prevent local vars from polluting window