Fixing race condition and other mistakes. Fixes #1603.

// FREEBIE