Added caching

I've added some caching here. I'm not 100% the else statement is working, or is the most efficient way of doing so. Let me know what you think.