Fixed the overlayImage function and modified functions depending on it