Fix busy-loop on upload error (and avoid a fow other race conditions).
Merge pull request #3 from maksverver/master

Fix busy-loop on upload error (and avoid a few other race conditions).