Moved variable update to fix race condition - fixes #969