Added some checks to prevent out of memory errors
Cleaned up code. Images in current view get loaded first