Fix issue 319 

Code by @dagolden . Just took the time to prepare the Pull Request.
I do not like much the idea of the global variable, but I lack the knowledge to suggest a better solution.

Ah, this solved issue 319.

@dagolden++