Fix race condition in img loader