Implemented Image Caching and cleaned up the code