Moved variable update to fix race condition - fixes #969
Merge pull request #977 from grumpydev/Fix-969

Moved variable update to fix race condition - fixes #969