Simplified code, added preloading of images