Use static variable over global variable.

This is a backport of code previously sent to github in 2011,
but not yet in master.