Port script to run locally on the web server. Fix race conditions.