changes from Nick Waterman <laptop@noseynick.com> to fix race condition in