Fixed race condition bug in the script, some typo fixes