converted global js vars to local vars where possible