Fixing bug when swtiching images and with window resize events