Added caching and fixed up some other stuff