Fix race condition in which the image would load before the onload callback was set