program works but flips images and doesnt like my boundary rules